liblscp 1.0.0

thread.h
Go to the documentation of this file.
1// thread.h
2//
3/****************************************************************************
4 liblscp - LinuxSampler Control Protocol API
5 Copyright (C) 2004-2021, rncbc aka Rui Nuno Capela. All rights reserved.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21*****************************************************************************/
22
23#ifndef __LSCP_THREAD_H
24#define __LSCP_THREAD_H
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#if (defined(_WIN32) || defined(__WIN32__))
31#if (!defined(WIN32))
32#define WIN32
33#endif
34#endif
35
36#if defined(WIN32)
37#include <windows.h>
38#else
39#include <pthread.h>
40#endif
41
42#include "lscp/version.h"
43
44#if defined(__cplusplus)
45 extern "C" {
46#endif
47
48//-------------------------------------------------------------------------
49// Status.
50
51 typedef enum _lscp_status_t
52{
53 LSCP_OK = 0,
54 LSCP_FAILED = -1,
55 LSCP_ERROR = -2,
56 LSCP_WARNING = -3,
57 LSCP_TIMEOUT = -4,
58 LSCP_QUIT = -5
59
60} lscp_status_t;
61
62//-------------------------------------------------------------------------
63// Mutexes.
64
65#if defined(WIN32)
66 typedef HANDLE lscp_mutex_t;
67#define lscp_mutex_init(m) { (m) = CreateMutex(NULL, 0, NULL); }
68#define lscp_mutex_destroy(m) if (m) { CloseHandle(m); }
69#define lscp_mutex_lock(m) WaitForSingleObject((m), INFINITE)
70#define lscp_mutex_unlock(m) ReleaseMutex(m)
71#else
72 typedef pthread_mutex_t lscp_mutex_t;
73#define lscp_mutex_init(m) pthread_mutex_init(&(m), NULL)
74#define lscp_mutex_destroy(m) pthread_mutex_destroy(&(m))
75#define lscp_mutex_lock(m) pthread_mutex_lock(&(m))
76#define lscp_mutex_unlock(m) pthread_mutex_unlock(&(m))
77#endif
78
79//-------------------------------------------------------------------------
80// Simple condition variables (FIXME: probably incorrect on WIN32).
81
82#if defined(WIN32)
83 typedef HANDLE lscp_cond_t;
84#define lscp_cond_init(c) { (c) = CreateEvent(NULL, FALSE, FALSE, NULL); }
85#define lscp_cond_destroy(c) if (c) { CloseHandle(c); }
86#define lscp_cond_wait(c, m) { lscp_mutex_unlock(m); WaitForSingleObject((c), INFINITE); lscp_mutex_lock(m); }
87#define lscp_cond_signal(c) SetEvent(c)
88#else
89 typedef pthread_cond_t lscp_cond_t;
90#define lscp_cond_init(c) pthread_cond_init(&(c), NULL)
91#define lscp_cond_destroy(c) pthread_cond_destroy(&(c))
92#define lscp_cond_wait(c, m) pthread_cond_wait(&(c), &(m))
93#define lscp_cond_signal(c) pthread_cond_signal(&(c))
94#endif
95
96//-------------------------------------------------------------------------
97// Threads.
98
99 struct _lscp_thread_t;
100
101 typedef void (*lscp_thread_proc_t)(void *pvData);
102
103 typedef struct _lscp_thread_t lscp_thread_t;
104
105 lscp_thread_t *lscp_thread_create (lscp_thread_proc_t pfnProc, void *pvData, int iDetach);
106 lscp_status_t lscp_thread_join (lscp_thread_t *pThread);
107 lscp_status_t lscp_thread_cancel (lscp_thread_t *pThread);
108 lscp_status_t lscp_thread_destroy (lscp_thread_t *pThread);
109
110#if defined(WIN32)
111#define lscp_thread_exit() ExitThread(0)
112#else
113#define lscp_thread_exit() pthread_exit(NULL)
114#endif
115
116#if defined(__cplusplus)
117}
118#endif
119
120#endif // __LSCP_THREAD_H
121
122// end of thread.h
_lscp_thread_t
Definition: thread.c:29
_lscp_thread_t::pfnProc
lscp_thread_proc_t pfnProc
Definition: thread.c:36
_lscp_thread_t::pvData
void * pvData
Definition: thread.c:37
_lscp_thread_t::iDetach
int iDetach
Definition: thread.c:38
lscp_thread_cancel
lscp_status_t lscp_thread_cancel(lscp_thread_t *pThread)
Definition: thread.c:135
lscp_thread_proc_t
void(* lscp_thread_proc_t)(void *pvData)
Definition: thread.h:101
lscp_status_t
enum _lscp_status_t lscp_status_t
lscp_mutex_t
pthread_mutex_t lscp_mutex_t
Definition: thread.h:72
lscp_cond_t
pthread_cond_t lscp_cond_t
Definition: thread.h:89
lscp_thread_join
lscp_status_t lscp_thread_join(lscp_thread_t *pThread)
Definition: thread.c:110
lscp_thread_create
lscp_thread_t * lscp_thread_create(lscp_thread_proc_t pfnProc, void *pvData, int iDetach)
Definition: thread.c:63
_lscp_status_t
_lscp_status_t
Definition: thread.h:52
LSCP_OK
@ LSCP_OK
Definition: thread.h:53
LSCP_QUIT
@ LSCP_QUIT
Definition: thread.h:58
LSCP_TIMEOUT
@ LSCP_TIMEOUT
Definition: thread.h:57
LSCP_ERROR
@ LSCP_ERROR
Definition: thread.h:55
LSCP_WARNING
@ LSCP_WARNING
Definition: thread.h:56
LSCP_FAILED
@ LSCP_FAILED
Definition: thread.h:54
lscp_thread_destroy
lscp_status_t lscp_thread_destroy(lscp_thread_t *pThread)
Definition: thread.c:160
version.h