summaryrefslogtreecommitdiffstats
path: root/core/Thread.h
blob: bf343214d582f6f6e4d637c3eb3e63eea198266c (plain)
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
/* $Id: Thread.h,v 1.10 2004/03/16 16:14:03 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://handbrake.m0k.org/>.
   It may be used under the terms of the GNU General Public License. */

#ifndef HB_THREAD_H
#define HB_THREAD_H

/* System headers */
#if defined( HB_BEOS )
#  include <OS.h>
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
#  include <pthread.h>
#elif defined( HB_CYGWIN )
#  include <windows.h>
#endif

#include "Utils.h"

/* Thread priorities */
#if defined( HB_BEOS )
#  define HB_LOW_PRIORITY    5
#  define HB_NORMAL_PRIORITY 10
#elif defined( HB_MACOSX )
#  define HB_LOW_PRIORITY    0
#  define HB_NORMAL_PRIORITY 31
#elif defined( HB_LINUX ) || defined( HB_CYGWIN )
/* Actually unused */
#  define HB_LOW_PRIORITY    0
#  define HB_NORMAL_PRIORITY 0
#endif

/**********************************************************************
 * HBThread/HBLock/HBCond declarations
 **********************************************************************/
HBThread *           HBThreadInit( char * name,
                                   void (* function)(void *),
                                   void * arg, int priority );
void                 HBThreadClose( HBThread ** );

HBLock             * HBLockInit();
static inline void   HBLockLock( HBLock * );
static inline void   HBLockUnlock( HBLock * );
void                 HBLockClose( HBLock ** );

HBCond             * HBCondInit();
static inline void   HBCondWait( HBCond *, HBLock * );
static inline void   HBCondSignal( HBCond * );
void                 HBCondClose( HBCond ** );


/**********************************************************************
 * HBLock implementation (inline functions)
 **********************************************************************/
struct HBLock
{
#if defined( HB_BEOS )
    sem_id          sem;
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
    pthread_mutex_t mutex;
#elif defined( HB_CYGWIN )
    HANDLE          mutex;
#endif
};

static inline void HBLockLock( HBLock * l )
{
#if defined( HB_BEOS )
    acquire_sem( l->sem );
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
    pthread_mutex_lock( &l->mutex );
#elif defined( HB_CYGWIN )
    WaitForSingleObject( l->mutex, INFINITE );
#endif
}

static inline void HBLockUnlock( HBLock * l )
{
#if defined( HB_BEOS )
    release_sem( l->sem );
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
    pthread_mutex_unlock( &l->mutex );
#elif defined( HB_CYGWIN )
    ReleaseMutex( l->mutex );
#endif
}


/**********************************************************************
 * HBCond implementation (inline functions)
 **********************************************************************/
struct HBCond
{
#if defined( HB_BEOS )
    int                 thread;
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
    pthread_cond_t      cond;
#elif defined( HB_CYGWIN )
    HANDLE              event;
#endif

};

static inline void HBCondWait( HBCond * c, HBLock * lock )
{
#if defined( HB_BEOS )
    c->thread = find_thread( NULL );
    release_sem( lock->sem );
    suspend_thread( c->thread );
    acquire_sem( lock->sem );
    c->thread = -1;
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
    pthread_cond_wait( &c->cond, &lock->mutex );
#elif defined( HB_CYGWIN )
    SignalObjectAndWait( lock->mutex, c->event, INFINITE, FALSE );
    WaitForSingleObject( lock->mutex, INFINITE );
#endif
}

static inline void HBCondSignal( HBCond * c )
{
#if defined( HB_BEOS )
    while( c->thread != -1 )
    {
        thread_info info;
        get_thread_info( c->thread, &info );
        if( info.state == B_THREAD_SUSPENDED )
        {
            resume_thread( c->thread );
            break;
        }
        /* In case HBCondSignal is called between HBCondWait's
           release_sem() and suspend_thread() lines, wait a bit */
        snooze( 5000 );
    }
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
    pthread_cond_signal( &c->cond );
#elif defined( HB_CYGWIN )
    PulseEvent( c->event );
#endif
}

#endif