Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
ThreadLib.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Embedded Web-Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: ThreadLib.h 5978 2026-09-11 16:13:48Z 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
40#ifndef ThreadLib_hpp
41
42#include <TargConfig.h>
43
61typedef enum {
62 ThreadPrioLowest,
63 ThreadPrioLow,
64 ThreadPrioNormal,
65 ThreadPrioHigh,
66 ThreadPrioHighest
68
69
70#ifndef __DOXYGEN__
71struct HttpRequest;
72struct ThreadMutex;
73struct Thread;
74#endif
75
81typedef void (*Thread_Run)(struct Thread* th);
82
83#ifdef __cplusplus
84extern "C" {
85#else
87#endif
88
89BA_API void ThreadReleaseLock_internalConstructor(
90 struct ThreadReleaseLock* o, struct HttpRequest* req);
91
92#ifdef __cplusplus
93}
94#endif
95
96
97
98
99#if defined(__cplusplus)
100
101#ifndef __DOXYGEN__
102struct ThreadReleaseLock;
103#endif
104
124{
128
132
134 ~ThreadLock();
135 private:
136 friend struct ThreadReleaseLock;
137 struct ThreadMutex* mutex;
138};
139
140
157{
160 ThreadReleaseLock(struct HttpRequest* req);
175 private:
176 struct ThreadMutex* mutex;
177};
178
179
180#else
181#define ThreadMutexBase ThreadMutex
182#define ThreadSemaphoreBase ThreadSemaphore
183#define ThreadBase Thread
184#endif
185
186
187#include "ThreadLibArch.h"
188
189#if defined(__cplusplus)
190
195struct ThreadMutex : public ThreadMutexBase
196{
197 void *operator new(size_t s) { return ::baMalloc(s); }
198 void operator delete(void* d) { if(d) ::baFree(d); }
199 void *operator new(size_t, void *place) { return place; }
200 void operator delete(void*, void *) { }
201
204 ThreadMutex() { ThreadMutex_constructor(this); }
207 ~ThreadMutex() { ThreadMutex_destructor(this); }
210 void set() { ThreadMutex_set(this); }
213 void release() { ThreadMutex_release(this); }
218 bool isOwner() { return ThreadMutex_isOwner(this)?true:false; }
219};
220
227struct ThreadSemaphore : public ThreadSemaphoreBase
228{
229 void *operator new(size_t s) { return ::baMalloc(s); }
230 void operator delete(void* d) { if(d) ::baFree(d); }
231 void *operator new(size_t, void *place) { return place; }
232 void operator delete(void*, void *) { }
233
237 ThreadSemaphore(){ ThreadSemaphore_constructor(this); }
241 ~ThreadSemaphore(void) { ThreadSemaphore_destructor(this); }
242
246 void wait() { ThreadSemaphore_wait(this); }
247
252 void signal() { ThreadSemaphore_signal(this); }
253};
254
255
260struct Thread : public ThreadBase
261{
264 void *operator new(size_t s) { return ::baMalloc(s); }
265 void operator delete(void* d) { if(d) ::baFree(d); }
266 void *operator new(size_t, void *place) { return place; }
267 void operator delete(void*, void *) { }
268
280 Thread(Thread_Run r, ThreadPriority priority, int stackSize) {
281 Thread_constructor(this, r, priority, stackSize); }
285 ~Thread() { Thread_destructor(this); }
289 void start() { Thread_start(this); }
290
296 static void sleep(unsigned int milliseconds) {
297 Thread_sleep(milliseconds); }
298};
299
300inline ThreadLock::ThreadLock(ThreadMutex& m) : mutex(&m) { mutex->set(); }
301inline ThreadLock::ThreadLock(ThreadMutex* m) : mutex(m) { mutex->set(); }
302inline ThreadLock::~ThreadLock() { mutex->release(); }
303
305 ThreadReleaseLock_internalConstructor(this, req);
306}
308 mutex(&m) { mutex->release(); }
310 mutex(m) { mutex->release(); }
312 mutex(tl.mutex) { mutex->release(); }
314 mutex(tl->mutex) { mutex->release(); }
316
317#endif
318 /* end of ThreadLib group */
320
321#endif
void * baMalloc(size_t size)
Allocate uninitialized storage using the target's configured allocator.
void baFree(void *p)
Release storage using the target's configured allocator.
ThreadLock(ThreadMutex &m)
Acquire the mutex until scope exit.
Definition: ThreadLib.h:300
~ThreadLock()
Unlock the mutex.
Definition: ThreadLib.h:302
ThreadPriority
Portable priority choices, mapped to the target operating system.
Definition: ThreadLib.h:61
~ThreadReleaseLock()
Reacquire the same mutex before leaving scope.
Definition: ThreadLib.h:315
void(* Thread_Run)(struct Thread *th)
Entry point invoked in the new thread after Thread::start.
Definition: ThreadLib.h:81
ThreadReleaseLock(struct HttpRequest *req)
Temporarily release the owned mutex; reacquire at scope exit.
Definition: ThreadLib.h:304
The HttpServer creates an HttpRequest object when the HttpServer parses a client request.
Definition: HttpServer.h:950
This class provides an easy and less error-prone way of locking and releasing a mutex.
Definition: ThreadLib.h:124
A mutual exclusion class.
Definition: ThreadLib.h:196
void release()
Release a mutex owned by this thread (C: ThreadMutex_release).
Definition: ThreadLib.h:213
ThreadMutex()
Initialize target mutex resources (C: ThreadMutex_constructor).
Definition: ThreadLib.h:204
~ThreadMutex()
Release target resources after all owners and waiters have stopped (C: ThreadMutex_destructor).
Definition: ThreadLib.h:207
void set()
Wait until the mutex can be acquired (C: ThreadMutex_set).
Definition: ThreadLib.h:210
bool isOwner()
Definition: ThreadLib.h:218
This class provides an easy and less error-prone way of temporarily releasing a locked mutex.
Definition: ThreadLib.h:157
A simple semaphore implementation.
Definition: ThreadLib.h:228
void wait()
Block until a notification can be consumed (C: ThreadSemaphore_wait).
Definition: ThreadLib.h:246
~ThreadSemaphore(void)
Release target resources only after signalers and waiters have stopped (C: ThreadSemaphore_destructor...
Definition: ThreadLib.h:241
ThreadSemaphore()
Create an initially unsignaled notification object (C: ThreadSemaphore_constructor).
Definition: ThreadLib.h:237
void signal()
Signal a waiter or make a notification available for a later wait (C: ThreadSemaphore_signal).
Definition: ThreadLib.h:252
A simple thread class.
Definition: ThreadLib.h:261
void start()
Release the constructed thread to run its entry point once (C: Thread_start).
Definition: ThreadLib.h:289
~Thread()
Release platform thread resources (C: Thread_destructor).
Definition: ThreadLib.h:285
Thread()
Uninitialized storage; call Thread_constructor before use/destruction.
Definition: ThreadLib.h:263
Thread(Thread_Run r, ThreadPriority priority, int stackSize)
Create a thread.
Definition: ThreadLib.h:280
static void sleep(unsigned int milliseconds)
Delay the calling thread (C: Thread_sleep).
Definition: ThreadLib.h:296