File: threads_kernel_1.h

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (277 lines) | stat: -rw-r--r-- 8,228 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (C) 2003  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_THREADS_KERNEl_1_
#define DLIB_THREADS_KERNEl_1_

#ifdef DLIB_ISO_CPP_ONLY
#error "DLIB_ISO_CPP_ONLY is defined so you can't use this OS dependent code.  Turn DLIB_ISO_CPP_ONLY off if you want to use it."
#endif

#include "threads_kernel_abstract.h"

#include "../windows_magic.h"
#include <windows.h>
#include "../algs.h"


namespace dlib
{

// ----------------------------------------------------------------------------------------
    
    typedef DWORD thread_id_type;

    inline thread_id_type get_thread_id (
    )
    {
        return GetCurrentThreadId();
    }

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
    // mutex object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    // forward declaration of signaler
    class signaler;

    class mutex
    {
        // give signaler access to hMutex
        friend class signaler;
    public:

        mutex (
        ) :
            hMutex(CreateMutex(NULL,FALSE,NULL))
        {
            if (hMutex == NULL)
            {
                throw dlib::thread_error(ECREATE_MUTEX,
        "in function mutex::mutex() an error occurred making the mutex"
                );           
            }
        }

        ~mutex (
        ) { CloseHandle(hMutex); }

        void lock (
        ) const { WaitForSingleObject (hMutex,INFINITE); }

        void unlock (
        ) const { ReleaseMutex(hMutex); }

    private:

        mutable HANDLE hMutex;

        // restricted functions
        mutex(mutex&);        // copy constructor
        mutex& operator=(mutex&);    // assignment operator
    };

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
    // signaler object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    class signaler
    {

    public:
        signaler (
            const mutex& associated_mutex
        ) :
            hSemaphore(CreateSemaphore (NULL, 0, 100000000, NULL)),
            waiters(0),
            hWaitersMutex(CreateMutex(NULL,FALSE,NULL)),
            hCountSema(CreateSemaphore (NULL,0,100000000,NULL)),
            m(associated_mutex)
        {           
            if (hSemaphore == NULL || hWaitersMutex == NULL || hCountSema == NULL)
            {
                if (hSemaphore != NULL)
                {
                    CloseHandle(hSemaphore); 
                }

                if (hWaitersMutex != NULL)
                {
                    CloseHandle(hWaitersMutex); 
                }

                if (hCountSema != NULL)
                {
                    CloseHandle(hCountSema); 
                }

                throw dlib::thread_error(ECREATE_SIGNALER,
        "in function signaler::signaler() an error occurred making the signaler"
                );        
            }
        }

        ~signaler (
        ) { CloseHandle(hSemaphore); CloseHandle(hWaitersMutex); CloseHandle(hCountSema);}

        void wait (
        ) const
        { 
            // get a lock on the mutex for the waiters variable
            WaitForSingleObject (hWaitersMutex,INFINITE);
            // mark that one more thread will be waiting on this signaler
            ++waiters;
            // release the mutex for waiters
            ReleaseMutex(hWaitersMutex);

            // release the assocaited mutex
            ReleaseMutex(m.hMutex);

            // wait for the semaphore to be signaled
            WaitForSingleObject (hSemaphore,INFINITE);

            // signal that we are awake
            ReleaseSemaphore(hCountSema,(LONG)1,NULL);

            // relock the associated mutex 
            WaitForSingleObject (m.hMutex,INFINITE);  
        }

        bool wait_or_timeout (
            unsigned long milliseconds
        ) const
        { 
            // get a lock on the mutex for the waiters variable
            WaitForSingleObject (hWaitersMutex,INFINITE);
            // mark that one more thread will be waiting on this signaler
            ++waiters;
            // release the mutex for waiters
            ReleaseMutex(hWaitersMutex);

            // release the assocaited mutex
            ReleaseMutex(m.hMutex);

            bool value;

            // wait for the semaphore to be signaled
            if ( WaitForSingleObject (hSemaphore, milliseconds ) == WAIT_TIMEOUT )
            {
                // in this case we should decrement waiters because we are returning
                // due to a timeout rather than because someone called signal() or 
                // broadcast().
                value = false;

                // get a lock on the mutex for the waiters variable
                WaitForSingleObject (hWaitersMutex,INFINITE);
                // mark that one less thread will be waiting on this signaler. 
                if (waiters != 0)
                    --waiters;
                // release the mutex for waiters
                ReleaseMutex(hWaitersMutex);
            }
            else 
            {
                value = true;
            }

            // signal that we are awake
            ReleaseSemaphore(hCountSema,(LONG)1,NULL);

            // relock the associated mutex 
            WaitForSingleObject (m.hMutex,INFINITE);  

            return value;
        }

        void signal (
        ) const 
        { 
            // get a lock on the mutex for the waiters variable
            WaitForSingleObject (hWaitersMutex,INFINITE);
            
            if (waiters > 0)
            {
                --waiters;
                // make the semaphore release one waiting thread
                ReleaseSemaphore(hSemaphore,1,NULL);

                // wait for signaled thread to wake up
                WaitForSingleObject(hCountSema,INFINITE);               
            }

            // release the mutex for waiters
            ReleaseMutex(hWaitersMutex);             
        }

        void broadcast (
        ) const 
        { 
            // get a lock on the mutex for the waiters variable
            WaitForSingleObject (hWaitersMutex,INFINITE);
            
            if (waiters > 0)
            {   
                // make the semaphore release all the waiting threads
                ReleaseSemaphore(hSemaphore,(LONG)waiters,NULL);

                // wait for count to be zero
                for (unsigned long i = 0; i < waiters; ++i)
                {
                    WaitForSingleObject(hCountSema,INFINITE);
                }

                waiters = 0;
            }

            // release the mutex for waiters
            ReleaseMutex(hWaitersMutex);               
        }

        const mutex& get_mutex (
        ) const { return m; }

    private:

        mutable HANDLE hSemaphore;

        mutable unsigned long waiters;
        mutable HANDLE hWaitersMutex;
        

        mutable HANDLE hCountSema;

        const mutex& m;

        // restricted functions
        signaler(signaler&);        // copy constructor
        signaler& operator=(signaler&);    // assignment operator
    };

// ----------------------------------------------------------------------------------------

    namespace threads_kernel_shared_helpers
    {
        bool spawn_thread (
            void (*funct)(void*),
            void* param
        );
        /*!
            is identical to create_new_thread() but just doesn't use any thread pooling.
        !*/
    }

// ----------------------------------------------------------------------------------------

}

#include "threads_kernel_shared.h"

#ifdef NO_MAKEFILE
#include "threads_kernel_1.cpp"
#endif

#endif // DLIB_THREADS_KERNEl_1_