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