~siretart/xine-lib/ubuntu

« back to all changes in this revision

Viewing changes to win32/contrib/pthreads/implement.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-12-15 13:13:45 UTC
  • mfrom: (0.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051215131345-8n4osv1j7fy9c1s1
* SECURITY UPDATE: Fix arbitrary code execution with crafted PNG images in
  embedded ffmpeg copy.
* src/libffmpeg/libavcodec/utils.c, avcodec_default_get_buffer(): Apply
  upstream patch to fix buffer overflow on decoding of small PIX_FMT_PAL8
  PNG files.
* References:
  CVE-2005-4048
  http://mplayerhq.hu/pipermail/ffmpeg-devel/2005-November/005333.html
  http://www1.mplayerhq.hu/cgi-bin/cvsweb.cgi/ffmpeg/libavcodec/
  utils.c.diff?r1=1.161&r2=1.162&cvsroot=FFMpeg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * implement.h
3
 
 *
4
 
 * Definitions that don't need to be public.
5
 
 *
6
 
 * Keeps all the internals out of pthread.h
7
 
 *
8
 
 * Pthreads-win32 - POSIX Threads Library for Win32
9
 
 * Copyright (C) 1998
10
 
 *
11
 
 * This library is free software; you can redistribute it and/or
12
 
 * modify it under the terms of the GNU Library General Public
13
 
 * License as published by the Free Software Foundation; either
14
 
 * version 2 of the License, or (at your option) any later version.
15
 
 *
16
 
 * This library is distributed in the hope that it will be useful,
17
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 
 * Library General Public License for more details.
20
 
 *
21
 
 * You should have received a copy of the GNU Library General Public
22
 
 * License along with this library; if not, write to the Free
23
 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24
 
 * MA 02111-1307, USA
25
 
 */
26
 
 
27
 
#ifndef _IMPLEMENT_H
28
 
#define _IMPLEMENT_H
29
 
 
30
 
#if !defined(malloc)
31
 
#include <malloc.h>
32
 
#endif
33
 
 
34
 
#if !defined(INT_MAX)
35
 
#include <limits.h>
36
 
#endif
37
 
 
38
 
/* use local include files during development */
39
 
#include "semaphore.h"
40
 
#include "sched.h"
41
 
 
42
 
#if defined(HAVE_C_INLINE) || defined(__cplusplus)
43
 
#define INLINE inline
44
 
#else
45
 
#define INLINE
46
 
#endif
47
 
 
48
 
typedef enum {
49
 
  /*
50
 
   * This enumeration represents the state of the thread;
51
 
   * The thread is still "alive" if the numeric value of the
52
 
   * state is greater or equal "PThreadStateRunning".
53
 
   */
54
 
  PThreadStateInitial = 0,      /* Thread not running                   */
55
 
  PThreadStateRunning,          /* Thread alive & kicking               */
56
 
  PThreadStateSuspended,        /* Thread alive but suspended           */
57
 
  PThreadStateCanceling,        /* Thread alive but and is              */
58
 
                                /* in the process of terminating        */
59
 
                                /* due to a cancellation request        */
60
 
  PThreadStateException,        /* Thread alive but exiting             */
61
 
                                /* due to an exception                  */
62
 
  PThreadStateLast
63
 
}
64
 
PThreadState;
65
 
 
66
 
 
67
 
typedef enum {
68
 
  /*
69
 
   * This enumeration represents the reason why a thread has
70
 
   * terminated/is terminating.
71
 
   */
72
 
  PThreadDemisePeaceful = 0,    /* Death due natural causes     */
73
 
  PThreadDemiseCancelled,       /* Death due to user cancel     */
74
 
  PThreadDemiseException,       /* Death due to unhandled       */
75
 
                                /* exception                    */
76
 
  PThreadDemiseNotDead  /* I'm not dead!                */
77
 
}
78
 
PThreadDemise;
79
 
 
80
 
struct pthread_t_ {
81
 
#ifdef _UWIN
82
 
  DWORD dummy[5];
83
 
#endif
84
 
  DWORD thread;
85
 
  HANDLE threadH;
86
 
  PThreadState state;
87
 
  PThreadDemise demise;
88
 
  void *exitStatus;
89
 
  void *parms;
90
 
  int ptErrno;
91
 
  int detachState;
92
 
  pthread_mutex_t cancelLock;  /* Used for async-cancel safety */
93
 
  int cancelState;
94
 
  int cancelType;
95
 
  HANDLE cancelEvent;
96
 
#ifdef __CLEANUP_C
97
 
  jmp_buf start_mark;
98
 
#endif /* __CLEANUP_C */
99
 
#if HAVE_SIGSET_T
100
 
  sigset_t sigmask;
101
 
#endif /* HAVE_SIGSET_T */
102
 
  int implicit:1;
103
 
  void *keys;
104
 
};
105
 
 
106
 
 
107
 
/* 
108
 
 * Special value to mark attribute objects as valid.
109
 
 */
110
 
#define PTW32_ATTR_VALID ((unsigned long) 0xC4C0FFEE)
111
 
 
112
 
struct pthread_attr_t_ {
113
 
  unsigned long valid;
114
 
  void *stackaddr;
115
 
  size_t stacksize;
116
 
  int detachstate;
117
 
  struct sched_param param;
118
 
  int inheritsched;
119
 
#if HAVE_SIGSET_T
120
 
  sigset_t sigmask;
121
 
#endif /* HAVE_SIGSET_T */
122
 
};
123
 
 
124
 
 
125
 
/*
126
 
 * ====================
127
 
 * ====================
128
 
 * Semaphores, Mutexes and Condition Variables
129
 
 * ====================
130
 
 * ====================
131
 
 */
132
 
 
133
 
struct sem_t_ {
134
 
#ifdef NEED_SEM
135
 
  unsigned int  value;
136
 
  CRITICAL_SECTION sem_lock_cs;
137
 
  HANDLE        event;
138
 
#else /* NEED_SEM */
139
 
  HANDLE sem;
140
 
#endif /* NEED_SEM */
141
 
};
142
 
 
143
 
#define PTW32_OBJECT_AUTO_INIT ((void *) -1)
144
 
#define PTW32_OBJECT_INVALID   NULL
145
 
 
146
 
struct pthread_mutex_t_ {
147
 
  LONG lock_idx;
148
 
  int recursive_count;
149
 
  int kind;
150
 
  pthread_t ownerThread;
151
 
  HANDLE wait_sema;
152
 
  CRITICAL_SECTION try_lock_cs;
153
 
};
154
 
 
155
 
struct pthread_mutexattr_t_ {
156
 
  int pshared;
157
 
  int kind;
158
 
};
159
 
 
160
 
/*
161
 
 * Possible values, other than PTW32_OBJECT_INVALID,
162
 
 * for the "interlock" element in a spinlock.
163
 
 *
164
 
 * In this implementation, when a spinlock is initialised,
165
 
 * the number of cpus available to the process is checked.
166
 
 * If there is only one cpu then "interlock" is set equal to
167
 
 * PTW32_SPIN_USE_MUTEX and u.mutex is a initialised mutex.
168
 
 * If the number of cpus is greater than 1 then "interlock"
169
 
 * is set equal to PTW32_SPIN_UNLOCKED and the number is
170
 
 * stored in u.cpus. This arrangement allows the spinlock
171
 
 * routines to attempt an InterlockedCompareExchange on "interlock"
172
 
 * immediately and, if that fails, to try the inferior mutex.
173
 
 *
174
 
 * "u.cpus" isn't used for anything yet, but could be used at
175
 
 * some point to optimise spinlock behaviour.
176
 
 */
177
 
#define PTW32_SPIN_UNLOCKED    (1)
178
 
#define PTW32_SPIN_LOCKED      (2)
179
 
#define PTW32_SPIN_USE_MUTEX   (3)
180
 
 
181
 
struct pthread_spinlock_t_ {
182
 
  long interlock;              /* Locking element for multi-cpus. */
183
 
  union {
184
 
    int cpus;                  /* No. of cpus if multi cpus, or   */
185
 
    pthread_mutex_t mutex;     /* mutex if single cpu.            */
186
 
  } u;
187
 
};
188
 
 
189
 
struct pthread_barrier_t_ {
190
 
  unsigned int nCurrentBarrierHeight;
191
 
  unsigned int nInitialBarrierHeight;
192
 
  int iStep;
193
 
  int pshared;
194
 
  sem_t semBarrierBreeched[2];
195
 
};
196
 
 
197
 
struct pthread_barrierattr_t_ {
198
 
  int pshared;
199
 
};
200
 
 
201
 
struct pthread_key_t_ {
202
 
  DWORD key;
203
 
  void (*destructor) (void *);
204
 
  pthread_mutex_t threadsLock;
205
 
  void *threads;
206
 
};
207
 
 
208
 
 
209
 
typedef struct ThreadParms ThreadParms;
210
 
typedef struct ThreadKeyAssoc ThreadKeyAssoc;
211
 
 
212
 
struct ThreadParms {
213
 
  pthread_t tid;
214
 
  void *(*start) (void *);
215
 
  void *arg;
216
 
};
217
 
 
218
 
 
219
 
struct pthread_cond_t_ {
220
 
  long            nWaitersBlocked;   /* Number of threads blocked            */
221
 
  long            nWaitersGone;      /* Number of threads timed out          */
222
 
  long            nWaitersUnblocked; /* Number of threads unblocked          */
223
 
  long            nWaitersToUnblock; /* Number of threads to unblock         */
224
 
  sem_t           semBlockQueue;     /* Queue up threads waiting for the     */
225
 
                                     /*   condition to become signalled      */
226
 
  sem_t           semBlockLock;      /* Semaphore that guards access to      */
227
 
                                     /* | waiters blocked count/block queue  */
228
 
                                     /* +-> Mandatory Sync.LEVEL-1           */
229
 
  pthread_mutex_t mtxUnblockLock;    /* Mutex that guards access to          */
230
 
                                     /* | waiters (to)unblock(ed) counts     */
231
 
                                     /* +-> Optional* Sync.LEVEL-2           */
232
 
};
233
 
 
234
 
 
235
 
struct pthread_condattr_t_ {
236
 
  int pshared;
237
 
};
238
 
 
239
 
#define PTW32_RWLOCK_MAGIC 0xfacade2
240
 
 
241
 
struct pthread_rwlock_t_ {
242
 
  pthread_mutex_t   mtxExclusiveAccess;
243
 
  pthread_mutex_t   mtxSharedAccessCompleted;
244
 
  pthread_cond_t    cndSharedAccessCompleted;
245
 
  int               nSharedAccessCount;
246
 
  int               nExclusiveAccessCount;
247
 
  int               nCompletedSharedAccessCount;
248
 
  int               nMagic;
249
 
};
250
 
 
251
 
struct pthread_rwlockattr_t_ {
252
 
  int               pshared;
253
 
};
254
 
 
255
 
 
256
 
struct ThreadKeyAssoc {
257
 
  /*
258
 
   * Purpose:
259
 
   *      This structure creates an association between a
260
 
   *      thread and a key.
261
 
   *      It is used to implement the implicit invocation
262
 
   *      of a user defined destroy routine for thread
263
 
   *      specific data registered by a user upon exiting a
264
 
   *      thread.
265
 
   *
266
 
   * Attributes:
267
 
   *      lock
268
 
   *              protects access to the rest of the structure
269
 
   *
270
 
   *      thread
271
 
   *              reference to the thread that owns the association.
272
 
   *              As long as this is not NULL, the association remains
273
 
   *              referenced by the pthread_t.
274
 
   *
275
 
   *      key
276
 
   *              reference to the key that owns the association.
277
 
   *              As long as this is not NULL, the association remains
278
 
   *              referenced by the pthread_key_t.
279
 
   *
280
 
   *      nextKey
281
 
   *              The pthread_t->keys attribute is the head of a
282
 
   *              chain of associations that runs through the nextKey
283
 
   *              link. This chain provides the 1 to many relationship
284
 
   *              between a pthread_t and all pthread_key_t on which
285
 
   *              it called pthread_setspecific.
286
 
   *
287
 
   *      nextThread
288
 
   *              The pthread_key_t->threads attribute is the head of
289
 
   *              a chain of assoctiations that runs through the
290
 
   *              nextThreads link. This chain provides the 1 to many
291
 
   *              relationship between a pthread_key_t and all the 
292
 
   *              PThreads that have called pthread_setspecific for
293
 
   *              this pthread_key_t.
294
 
   *
295
 
   *
296
 
   * Notes:
297
 
   *      1)      As long as one of the attributes, thread or key, is
298
 
   *              not NULL, the association is being referenced; once
299
 
   *              both are NULL, the association must be released.
300
 
   *
301
 
   *      2)      Under WIN32, an association is only created by
302
 
   *              pthread_setspecific if the user provided a
303
 
   *              destroyRoutine when they created the key.
304
 
   *
305
 
   *
306
 
   */
307
 
  pthread_mutex_t lock;
308
 
  pthread_t thread;
309
 
  pthread_key_t key;
310
 
  ThreadKeyAssoc *nextKey;
311
 
  ThreadKeyAssoc *nextThread;
312
 
};
313
 
 
314
 
 
315
 
#ifdef __CLEANUP_SEH
316
 
/*
317
 
 * --------------------------------------------------------------
318
 
 * MAKE_SOFTWARE_EXCEPTION
319
 
 *      This macro constructs a software exception code following
320
 
 *      the same format as the standard Win32 error codes as defined
321
 
 *      in WINERROR.H
322
 
 *  Values are 32 bit values layed out as follows:
323
 
 *
324
 
 *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
325
 
 *  +---+-+-+-----------------------+-------------------------------+
326
 
 *  |Sev|C|R|     Facility          |               Code            |
327
 
 *  +---+-+-+-----------------------+-------------------------------+
328
 
 *
329
 
 * Severity Values:
330
 
 */
331
 
#define SE_SUCCESS              0x00
332
 
#define SE_INFORMATION          0x01
333
 
#define SE_WARNING              0x02
334
 
#define SE_ERROR                0x03
335
 
 
336
 
#define MAKE_SOFTWARE_EXCEPTION( _severity, _facility, _exception ) \
337
 
( (DWORD) ( ( (_severity) << 30 ) |     /* Severity code        */ \
338
 
            ( 1 << 29 ) |               /* MS=0, User=1         */ \
339
 
            ( 0 << 28 ) |               /* Reserved             */ \
340
 
            ( (_facility) << 16 ) |     /* Facility Code        */ \
341
 
            ( (_exception) <<  0 )      /* Exception Code       */ \
342
 
            ) )
343
 
 
344
 
/*
345
 
 * We choose one specific Facility/Error code combination to
346
 
 * identify our software exceptions vs. WIN32 exceptions.
347
 
 * We store our actual component and error code within
348
 
 * the optional information array.
349
 
 */
350
 
#define EXCEPTION_PTW32_SERVICES        \
351
 
     MAKE_SOFTWARE_EXCEPTION( SE_ERROR, \
352
 
                              PTW32_SERVICES_FACILITY, \
353
 
                              PTW32_SERVICES_ERROR )
354
 
 
355
 
#define PTW32_SERVICES_FACILITY         0xBAD
356
 
#define PTW32_SERVICES_ERROR            0xDEED
357
 
 
358
 
#endif /* __CLEANUP_SEH */
359
 
 
360
 
/*
361
 
 * Services available through EXCEPTION_PTW32_SERVICES
362
 
 * and also used [as parameters to ptw32_throw()] as
363
 
 * generic exception selectors.
364
 
 */
365
 
 
366
 
#define PTW32_EPS_EXIT                  (1)
367
 
#define PTW32_EPS_CANCEL                (2)
368
 
 
369
 
/* Mutex constants */
370
 
enum {
371
 
  PTW32_MUTEX_LOCK_IDX_INIT     = -1,
372
 
  PTW32_MUTEX_OWNER_ANONYMOUS = 1
373
 
};
374
 
 
375
 
 
376
 
/* Declared in global.c */
377
 
extern int ptw32_processInitialized;
378
 
extern pthread_key_t ptw32_selfThreadKey;
379
 
extern pthread_key_t ptw32_cleanupKey;
380
 
 
381
 
extern int ptw32_mutex_default_kind;
382
 
 
383
 
extern int ptw32_concurrency;
384
 
 
385
 
extern CRITICAL_SECTION ptw32_mutex_test_init_lock;
386
 
extern CRITICAL_SECTION ptw32_cond_test_init_lock;
387
 
extern CRITICAL_SECTION ptw32_rwlock_test_init_lock;
388
 
extern CRITICAL_SECTION ptw32_spinlock_test_init_lock;
389
 
 
390
 
#ifdef _UWIN
391
 
extern int pthread_count;
392
 
#endif
393
 
 
394
 
/* Declared in misc.c */
395
 
#ifdef NEED_CALLOC
396
 
#define calloc(n, s) ptw32_calloc(n, s)
397
 
void *ptw32_calloc(size_t n, size_t s);
398
 
#endif
399
 
 
400
 
/* Declared in private.c */
401
 
void ptw32_throw(DWORD exception);
402
 
 
403
 
#ifdef __cplusplus
404
 
extern "C" {
405
 
#endif /* __cplusplus */
406
 
 
407
 
/*
408
 
 * =====================
409
 
 * =====================
410
 
 * Forward Declarations
411
 
 * =====================
412
 
 * =====================
413
 
 */
414
 
int ptw32_processInitialize (void);
415
 
 
416
 
void ptw32_processTerminate (void);
417
 
 
418
 
void ptw32_threadDestroy (pthread_t tid);
419
 
 
420
 
void ptw32_pop_cleanup_all (int execute);
421
 
 
422
 
pthread_t ptw32_new (void);
423
 
 
424
 
#if ! defined (__MINGW32__) || defined (__MSVCRT__)
425
 
unsigned __stdcall
426
 
#else
427
 
void
428
 
#endif
429
 
ptw32_threadStart (void * vthreadParms);
430
 
 
431
 
void ptw32_callUserDestroyRoutines (pthread_t thread);
432
 
 
433
 
int ptw32_tkAssocCreate (ThreadKeyAssoc ** assocP,
434
 
                            pthread_t thread,
435
 
                            pthread_key_t key);
436
 
 
437
 
void ptw32_tkAssocDestroy (ThreadKeyAssoc * assoc);
438
 
 
439
 
int ptw32_sem_timedwait (sem_t * sem,
440
 
                            const struct timespec * abstime);
441
 
 
442
 
#ifdef NEED_SEM
443
 
void ptw32_decrease_semaphore(sem_t * sem);
444
 
BOOL ptw32_increase_semaphore(sem_t * sem,
445
 
                                 unsigned int n);
446
 
#endif /* NEED_SEM */
447
 
 
448
 
#ifdef __cplusplus
449
 
}
450
 
#endif /* __cplusplus */
451
 
 
452
 
 
453
 
#ifdef _UWIN_
454
 
#   ifdef       _MT
455
 
#       ifdef __cplusplus
456
 
        extern "C" {
457
 
#       endif
458
 
        _CRTIMP unsigned long  __cdecl _beginthread (void (__cdecl *) (void *),
459
 
                unsigned, void *);
460
 
        _CRTIMP void __cdecl _endthread(void);
461
 
        _CRTIMP unsigned long __cdecl _beginthreadex(void *, unsigned,
462
 
                unsigned (__stdcall *) (void *), void *, unsigned, unsigned *);
463
 
        _CRTIMP void __cdecl _endthreadex(unsigned);
464
 
#       ifdef __cplusplus
465
 
        }
466
 
#       endif
467
 
#   endif
468
 
#else
469
 
#   include <process.h>
470
 
#endif
471
 
 
472
 
/*
473
 
 * Check for old and new versions of cygwin. See the FAQ file:
474
 
 *
475
 
 * Question 1 - How do I get pthreads-win32 to link under Cygwin or Mingw32?
476
 
 *
477
 
 * Patch by Anders Norlander <anorland@hem2.passagen.se>
478
 
 */
479
 
#if defined(__CYGWIN32__) || defined(__CYGWIN__) || defined(NEED_CREATETHREAD)
480
 
 
481
 
/* 
482
 
 * Macro uses args so we can cast start_proc to LPTHREAD_START_ROUTINE
483
 
 * in order to avoid warnings because of return type
484
 
 */
485
 
 
486
 
#define _beginthreadex(security, \
487
 
                       stack_size, \
488
 
                       start_proc, \
489
 
                       arg, \
490
 
                       flags, \
491
 
                       pid) \
492
 
        CreateThread(security, \
493
 
                     stack_size, \
494
 
                     (LPTHREAD_START_ROUTINE) start_proc, \
495
 
                     arg, \
496
 
                     flags, \
497
 
                     pid)
498
 
 
499
 
#define _endthreadex ExitThread
500
 
 
501
 
#endif /* __CYGWIN32__ || __CYGWIN__ || NEED_CREATETHREAD*/
502
 
 
503
 
 
504
 
#endif /* _IMPLEMENT_H */
505