1/*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
3 * The contents of this file constitute Original Code as defined in and are
4 * subject to the Apple Public Source License Version 1.2 (the 'License').
5 * You may not use this file except in compliance with the License. Please
6 * obtain a copy of the License at http://www.apple.com/publicsource and
7 * read it before using this file.
8 *
9 * This Original Code and all software distributed under the License are
10 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
11 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
12 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
13 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please
14 * see the License for the specific language governing rights and
15 * limitations under the License.
16 */
17
18/******************************************************************
19
20	MUSCLE SmartCard Development ( http://www.linuxnet.com )
21	    Title  : thread_macosx.c
22	    Package: pcsc lite
23            Author : David Corcoran
24            Date   : 7/6/00
25	    License: Copyright (C) 2000 David Corcoran
26	             <corcoran@linuxnet.com>
27            Purpose: This handles thread function abstraction.
28
29********************************************************************/
30
31#include "config.h"
32#include "wintypes.h"
33#include "pcsclite.h"
34#include "thread_generic.h"
35
36#define PCSC_MUTEX_LOCKED    1
37#define PCSC_MUTEX_UNLOCKED  0
38
39int SYS_MutexInit(PCSCLITE_MUTEX_T mMutex)
40{
41	int retval;
42	retval = pthread_mutex_init(mMutex, NULL);
43	return retval;
44}
45
46int SYS_MutexDestroy(PCSCLITE_MUTEX_T mMutex)
47{
48	int retval;
49	retval = pthread_mutex_destroy(mMutex);
50	return retval;
51}
52
53int SYS_MutexLock(PCSCLITE_MUTEX_T mMutex)
54{
55	int retval;
56	retval = pthread_mutex_lock(mMutex);
57	return retval;
58}
59
60int SYS_MutexUnLock(PCSCLITE_MUTEX_T mMutex)
61{
62	int retval;
63	retval = pthread_mutex_unlock(mMutex);
64	return retval;
65}
66
67int SYS_ThreadCreate(PCSCLITE_THREAD_T * pthThread, int attributes,
68	PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
69{
70	pthread_attr_t attr;
71	int rx;
72
73	if (0 != pthread_attr_init(&attr))
74		return 0;
75
76	if (attributes & THREAD_ATTR_DETACHED)
77		if (0 != pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
78		{
79			pthread_attr_destroy(&attr);
80			return 0;
81		}
82
83	rx = pthread_create(pthThread, &attr, pvFunction, pvArg);
84
85	pthread_attr_destroy(&attr);
86
87	return (0 == rx);	// return 1 if success, 0 otherwise
88}
89
90int SYS_ThreadCancel(PCSCLITE_THREAD_T * pthThread)
91{
92
93	int retval;
94	retval = pthread_cancel(*pthThread);
95
96	if (retval == 0)
97	{
98		return 1;
99	} else
100	{
101		return 0;
102	}
103}
104
105int SYS_ThreadDetach(PCSCLITE_THREAD_T pthThread)
106{
107	// Returns 1 (true) if thread detached OK, 0 (false) otherwise
108	if (pthThread)
109		return (pthread_detach(pthThread) == 0);	// 0 result is success
110
111	return 0;
112}
113
114int SYS_ThreadJoin(PCSCLITE_THREAD_T *pthThread, LPVOID* pvRetVal)
115{
116
117	int retval;
118	retval = pthread_join(*pthThread, pvRetVal);
119
120	if (retval == 0)
121	{
122		return 1;
123	} else
124	{
125		return 0;
126	}
127}
128
129int SYS_ThreadExit(LPVOID pvRetVal)
130{
131
132	pthread_exit(pvRetVal);
133	return 1;
134}
135