1 /*
2 * ===========================
3 * VDK Visual Development Kit
4 * Version 0.6
5 * Jan 1999
6 * ===========================
7 *
8 * Copyright (C) 1999, Salmaso Raffaele
9 * Developed by Salmaso Raffaele <r.salmaso@flashnet.it>
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 Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
25 */
26
27 #include "vdk/vdkthread.h"
28
VDKMutex()29 VDKMutex::VDKMutex () {
30 pthread_mutex_init (&mutex, (pthread_mutexattr_t *) 0);
31 };
32
~VDKMutex()33 VDKMutex::~VDKMutex () {
34 pthread_mutex_destroy (&mutex);
35 };
36
Lock()37 VDKMutexEnum VDKMutex::Lock () {
38 if (pthread_mutex_trylock (&mutex) != 0)
39 return VDKMUTEX_ERROR;
40 return VDKMUTEX_OK;
41 }
42
Release()43 VDKMutexEnum VDKMutex::Release () {
44 if (pthread_mutex_unlock (&mutex) != 0)
45 return VDKMUTEX_ERROR;
46 return VDKMUTEX_OK;
47 };
48
Wait(long time)49 VDKMutexEnum VDKMutex::Wait (long time) {
50 if (pthread_mutex_lock (&mutex) != 0)
51 return VDKMUTEX_ERROR;
52 return VDKMUTEX_OK;
53 }
54
VDKThread()55 VDKThread::VDKThread () {
56 state = VDKTHREAD_IDLE;
57 }
58
~VDKThread()59 VDKThread::~VDKThread () {
60 }
61
Start(void * arg)62 VDKThreadEnum VDKThread::Start (void * arg)
63 {
64 if (state != VDKTHREAD_IDLE)
65 return VDKTHREAD_IS_YET_RUNNING;
66 state = VDKTHREAD_RUNNING;
67 if (arg == NULL)
68 {
69 if (pthread_create (&id, (pthread_attr_t *) 0,
70 VDKThread::EntryPoint,
71 (void *) this) != 0)
72 {
73 state = VDKTHREAD_IDLE;
74 return VDKTHREAD_CANNOT_CREATE;
75 }
76 }
77 else
78 {
79 if (pthread_create (&id, (pthread_attr_t *) 0,
80 (void * (*)(void *)) arg,
81 NULL) != 0) {
82 state = VDKTHREAD_IDLE;
83 return VDKTHREAD_CANNOT_CREATE;
84 }
85 }
86
87 return VDKTHREAD_RUNNING;
88 }
89
EntryPoint(void * arg)90 void * VDKThread::EntryPoint (void * arg) {
91 VDKThread * thread = (VDKThread *) arg;
92 thread->Execute ();
93 return NULL;
94 }
95
Execute()96 void VDKThread::Execute () {
97 }
98
Stop()99 VDKThreadEnum VDKThread::Stop () {
100 if (state == VDKTHREAD_RUNNING)
101 if (pthread_cancel (id) == 0)
102 state =VDKTHREAD_STOPPED;
103 return state;
104 }
105
Exit(void * status)106 void VDKThread::Exit (void * status) {
107 state = VDKTHREAD_EXITED;
108 pthread_exit (status);
109 }
110
Wait()111 void * VDKThread::Wait () {
112 void * status = 0;
113 if (state != VDKTHREAD_IDLE) {
114 pthread_join (id, &status);
115 state = VDKTHREAD_IDLE;
116 }
117 return status;
118 }
119