source: golgotha/src/i4/threads/linux/threads.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 14 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 2.0 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include <stdio.h>
10#include <pthread.h>
11#include "error/error.hh"
12#include "threads/threads.hh"
13#include "init/init.hh"
14#include <sched.h>
15
16static w32 i4_thread_count=0;
17static i4_critical_section_class i4_thread_lock;
18int i4_main_thread_id;
19
20void i4_wait_threads()  // waits for all threads to terminate (don't call from a thread!)
21{
22  while (i4_thread_count!=0)
23    i4_thread_yield();
24}
25 
26static i4_critical_section_class i4_thread_start_lock;
27static i4_thread_func_type i4_thread_to_start;
28static pthread_t handle;
29
30void *i4_thread_starter(void *arg)
31{
32  i4_thread_func_type start=i4_thread_to_start;
33  i4_thread_start_lock.unlock();
34
35  start(arg);
36 
37  i4_thread_lock.lock();
38  i4_thread_count--;
39  i4_thread_lock.unlock();
40
41  pthread_exit(0);
42 
43  return 0; 
44}
45
46
47
48void i4_add_thread(i4_thread_func_type fun, w32 stack_size, void *arg_list)
49{
50  i4_thread_lock.lock();
51  i4_thread_count++;
52  i4_thread_lock.unlock();
53
54  i4_thread_start_lock.lock();
55  i4_thread_to_start=fun;
56 
57  pthread_create(&handle, 0, i4_thread_starter, arg_list);
58}
59
60
61
62void i4_thread_yield()
63{
64  sched_yield();
65}
66
67
68int i4_get_thread_id()
69{
70  return pthread_self();
71}
72
73
74
75i4_critical_section_class::i4_critical_section_class(void)
76{
77  pthread_mutex_init(&mutex,0);
78}
79
80i4_critical_section_class::~i4_critical_section_class(void)
81{
82        pthread_mutex_destroy(&mutex);
83}
84
85void i4_critical_section_class::lock(void)
86{   
87        pthread_mutex_lock(&mutex);
88}
89
90void i4_critical_section_class::unlock(void)
91{
92        pthread_mutex_unlock(&mutex);
93}
94
Note: See TracBrowser for help on using the repository browser.