1 /* 2 * This is the header file for the module that implements some missing 3 * synchronization primitives from the Tcl API. 4 * 5 * Copyright (c) 2002 by Zoran Vasiljevic. 6 * 7 * See the file "license.txt" for information on usage and redistribution 8 * of this file, and for a DISCLAIMER OF ALL WARRANTIES. 9 * --------------------------------------------------------------------------- 10 */ 11 12 #ifndef _SP_H_ 13 #define _SP_H_ 14 15 #include "tclThreadInt.h" 16 17 /* 18 * The following structure defines a locking bucket. A locking 19 * bucket is associated with a mutex and protects access to 20 * objects stored in bucket hash table. 21 */ 22 23 typedef struct SpBucket { 24 Tcl_Mutex lock; /* For locking the bucket */ 25 Tcl_Condition cond; /* For waiting on threads to release items */ 26 Tcl_HashTable handles; /* Hash table of given-out handles in bucket */ 27 } SpBucket; 28 29 #define NUMSPBUCKETS 32 30 31 /* 32 * All types of mutexes share this common part. 33 */ 34 35 typedef struct Sp_AnyMutex_ { 36 int lockcount; /* If !=0 mutex is locked */ 37 int numlocks; /* Number of times the mutex got locked */ 38 Tcl_Mutex lock; /* Regular mutex */ 39 Tcl_ThreadId owner; /* Current lock owner thread (-1 = any) */ 40 } Sp_AnyMutex; 41 42 /* 43 * Implementation of the exclusive mutex. 44 */ 45 46 typedef struct Sp_ExclusiveMutex_ { 47 int lockcount; /* Flag: 1-locked, 0-not locked */ 48 int numlocks; /* Number of times the mutex got locked */ 49 Tcl_Mutex lock; /* Regular mutex */ 50 Tcl_ThreadId owner; /* Current lock owner thread */ 51 /* --- */ 52 Tcl_Mutex mutex; /* Mutex being locked */ 53 } Sp_ExclusiveMutex_; 54 55 typedef Sp_ExclusiveMutex_* Sp_ExclusiveMutex; 56 57 /* 58 * Implementation of the recursive mutex. 59 */ 60 61 typedef struct Sp_RecursiveMutex_ { 62 int lockcount; /* # of times this mutex is locked */ 63 int numlocks; /* Number of time the mutex got locked */ 64 Tcl_Mutex lock; /* Regular mutex */ 65 Tcl_ThreadId owner; /* Current lock owner thread */ 66 /* --- */ 67 Tcl_Condition cond; /* Wait to be allowed to lock the mutex */ 68 } Sp_RecursiveMutex_; 69 70 typedef Sp_RecursiveMutex_* Sp_RecursiveMutex; 71 72 /* 73 * Implementation of the read/writer mutex. 74 */ 75 76 typedef struct Sp_ReadWriteMutex_ { 77 int lockcount; /* >0: # of readers, -1: sole writer */ 78 int numlocks; /* Number of time the mutex got locked */ 79 Tcl_Mutex lock; /* Regular mutex */ 80 Tcl_ThreadId owner; /* Current lock owner thread */ 81 /* --- */ 82 unsigned int numrd; /* # of readers waiting for lock */ 83 unsigned int numwr; /* # of writers waiting for lock */ 84 Tcl_Condition rcond; /* Reader lockers wait here */ 85 Tcl_Condition wcond; /* Writer lockers wait here */ 86 } Sp_ReadWriteMutex_; 87 88 typedef Sp_ReadWriteMutex_* Sp_ReadWriteMutex; 89 90 91 /* 92 * API for exclusive mutexes. 93 */ 94 95 MODULE_SCOPE int Sp_ExclusiveMutexLock(Sp_ExclusiveMutex *mutexPtr); 96 MODULE_SCOPE int Sp_ExclusiveMutexIsLocked(Sp_ExclusiveMutex *mutexPtr); 97 MODULE_SCOPE int Sp_ExclusiveMutexUnlock(Sp_ExclusiveMutex *mutexPtr); 98 MODULE_SCOPE void Sp_ExclusiveMutexFinalize(Sp_ExclusiveMutex *mutexPtr); 99 100 /* 101 * API for recursive mutexes. 102 */ 103 104 MODULE_SCOPE int Sp_RecursiveMutexLock(Sp_RecursiveMutex *mutexPtr); 105 MODULE_SCOPE int Sp_RecursiveMutexIsLocked(Sp_RecursiveMutex *mutexPtr); 106 MODULE_SCOPE int Sp_RecursiveMutexUnlock(Sp_RecursiveMutex *mutexPtr); 107 MODULE_SCOPE void Sp_RecursiveMutexFinalize(Sp_RecursiveMutex *mutexPtr); 108 109 /* 110 * API for reader/writer mutexes. 111 */ 112 113 MODULE_SCOPE int Sp_ReadWriteMutexRLock(Sp_ReadWriteMutex *mutexPtr); 114 MODULE_SCOPE int Sp_ReadWriteMutexWLock(Sp_ReadWriteMutex *mutexPtr); 115 MODULE_SCOPE int Sp_ReadWriteMutexIsLocked(Sp_ReadWriteMutex *mutexPtr); 116 MODULE_SCOPE int Sp_ReadWriteMutexUnlock(Sp_ReadWriteMutex *mutexPtr); 117 MODULE_SCOPE void Sp_ReadWriteMutexFinalize(Sp_ReadWriteMutex *mutexPtr); 118 119 #endif /* _SP_H_ */ 120 121 /* EOF $RCSfile: threadSpCmd.h,v $ */ 122 123 /* Emacs Setup Variables */ 124 /* Local Variables: */ 125 /* mode: C */ 126 /* indent-tabs-mode: nil */ 127 /* c-basic-offset: 4 */ 128 /* End: */ 129