Barracuda Application Server C/C++ Reference
NO
ThreadLib.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Embedded Web-Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: ThreadLib.h 4915 2021-12-01 18:26:55Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2004 - 2008
17 *
18 * This software is copyrighted by and is the sole property of Real
19 * Time Logic LLC. All rights, title, ownership, or other interests in
20 * the software remain the property of Real Time Logic LLC. This
21 * software may only be used in accordance with the terms and
22 * conditions stipulated in the corresponding license agreement under
23 * which the software has been supplied. Any unauthorized use,
24 * duplication, transmission, distribution, or disclosure of this
25 * software is expressly forbidden.
26 *
27 * This Copyright notice may not be removed or modified without prior
28 * written consent of Real Time Logic LLC.
29 *
30 * Real Time Logic LLC. reserves the right to modify this software
31 * without notice.
32 *
33 * http://www.realtimelogic.com
34 ****************************************************************************
35 *
36 */
37
38#ifndef ThreadLib_hpp
39
40#include <TargConfig.h>
41
58typedef enum {
59 ThreadPrioLowest,
60 ThreadPrioLow,
61 ThreadPrioNormal,
62 ThreadPrioHigh,
63 ThreadPrioHighest
65
66
67#ifndef __DOXYGEN__
68struct HttpRequest;
69struct ThreadMutex;
70struct Thread;
71#endif
72
75typedef void (*Thread_Run)(struct Thread* th);
76
77#ifdef __cplusplus
78extern "C" {
79#else
81#endif
82
83BA_API void ThreadReleaseLock_internalConstructor(
84 struct ThreadReleaseLock* o, struct HttpRequest* req);
85
86#ifdef __cplusplus
87}
88#endif
89
90
91
92
93#if defined(__cplusplus)
94
95#ifndef __DOXYGEN__
97#endif
98
116{
119
122
124 ~ThreadLock();
125 private:
126 friend struct ThreadReleaseLock;
127 struct ThreadMutex* mutex;
128};
129
130
152{
154 ThreadReleaseLock(struct HttpRequest* req);
165 private:
166 struct ThreadMutex* mutex;
167};
168
169
170#else
171#define ThreadMutexBase ThreadMutex
172#define ThreadSemaphoreBase ThreadSemaphore
173#define ThreadBase Thread
174#endif
175
176
177#include "ThreadLibArch.h"
178
179#if defined(__cplusplus)
180
185struct ThreadMutex : public ThreadMutexBase
186{
187 void *operator new(size_t s) { return ::baMalloc(s); }
188 void operator delete(void* d) { if(d) ::baFree(d); }
189 void *operator new(size_t, void *place) { return place; }
190 void operator delete(void*, void *) { }
191
193 ThreadMutex() { ThreadMutex_constructor(this); }
195 ~ThreadMutex() { ThreadMutex_destructor(this); }
197 void set() { ThreadMutex_set(this); }
199 void release() { ThreadMutex_release(this); }
203 bool isOwner() { return ThreadMutex_isOwner(this)?true:false; }
204};
205
212struct ThreadSemaphore : public ThreadSemaphoreBase
213{
214 void *operator new(size_t s) { return ::baMalloc(s); }
215 void operator delete(void* d) { if(d) ::baFree(d); }
216 void *operator new(size_t, void *place) { return place; }
217 void operator delete(void*, void *) { }
218
221 ThreadSemaphore(){ ThreadSemaphore_constructor(this); }
224 ~ThreadSemaphore(void) { ThreadSemaphore_destructor(this); }
225
228 void wait() { ThreadSemaphore_wait(this); }
229
232 void signal() { ThreadSemaphore_signal(this); }
233};
234
235
240struct Thread : public ThreadBase
241{
242 Thread() {}
243 void *operator new(size_t s) { return ::baMalloc(s); }
244 void operator delete(void* d) { if(d) ::baFree(d); }
245 void *operator new(size_t, void *place) { return place; }
246 void operator delete(void*, void *) { }
247
253 Thread(Thread_Run r, ThreadPriority priority, int stackSize) {
254 Thread_constructor(this, r, priority, stackSize); }
255 ~Thread() { Thread_destructor(this); }
258 void start() { Thread_start(this); }
259
262 static void sleep(unsigned int milliseconds) {
263 Thread_sleep(milliseconds); }
264};
265
266inline ThreadLock::ThreadLock(ThreadMutex& m) : mutex(&m) { mutex->set(); }
267inline ThreadLock::ThreadLock(ThreadMutex* m) : mutex(m) { mutex->set(); }
268inline ThreadLock::~ThreadLock() { mutex->release(); }
269
271 ThreadReleaseLock_internalConstructor(this, req);
272}
274 mutex(&m) { mutex->release(); }
276 mutex(m) { mutex->release(); }
278 mutex(tl.mutex) { mutex->release(); }
280 mutex(tl->mutex) { mutex->release(); }
282
283#endif
284 /* end of ThreadLib group */
286
287#endif
void * baMalloc(size_t size)
Returns pointer to uninitialized newly-allocated space for an object of size "size",...
void baFree(void *p)
Deallocates space to which it points.
ThreadLock(ThreadMutex &m)
Lock a region of code.
Definition: ThreadLib.h:266
~ThreadLock()
Unlock the mutex.
Definition: ThreadLib.h:268
ThreadPriority
Thread priority list.
Definition: ThreadLib.h:58
~ThreadReleaseLock()
Lock the temporarily unlock mutex.
Definition: ThreadLib.h:281
void(* Thread_Run)(struct Thread *th)
Prototype for the thread run method.
Definition: ThreadLib.h:75
ThreadReleaseLock(struct HttpRequest *req)
Temporarily unlock a locked mutex from within a resource.
Definition: ThreadLib.h:270
The HttpServer creates an HttpRequest object when the HttpServer parses a client request.
Definition: HttpServer.h:808
This class provides an easy and less error prone way of locking and releasing a mutex.
Definition: ThreadLib.h:116
A mutual exclusion class.
Definition: ThreadLib.h:186
void release()
Release the mutex.
Definition: ThreadLib.h:199
ThreadMutex()
Create a mutex.
Definition: ThreadLib.h:193
~ThreadMutex()
Destroy a mutex.
Definition: ThreadLib.h:195
void set()
Lock the mutex.
Definition: ThreadLib.h:197
bool isOwner()
Returns true if the mutex is locked and the current thread is the owner.
Definition: ThreadLib.h:203
This class provides an easy and less error prone way of temporarily releasing a locked mutex.
Definition: ThreadLib.h:152
A simple semaphore implementation.
Definition: ThreadLib.h:213
void wait()
Wait (block) for another thread to signal (start) the thread.
Definition: ThreadLib.h:228
~ThreadSemaphore(void)
destroy the semaphore
Definition: ThreadLib.h:224
ThreadSemaphore()
Create a semaphore and set the counter to zero.
Definition: ThreadLib.h:221
void signal()
Signal a waiting (blocking) thread.
Definition: ThreadLib.h:232
A simple thread class.
Definition: ThreadLib.h:241
void start()
Start the thread.
Definition: ThreadLib.h:258
Thread(Thread_Run r, ThreadPriority priority, int stackSize)
Create a thread.
Definition: ThreadLib.h:253
static void sleep(unsigned int milliseconds)
Suspend the current thread for n milliseconds.
Definition: ThreadLib.h:262