summaryrefslogtreecommitdiffstats
path: root/flow/gsl/gslartsthreads.cc
blob: 9bd89e561e2d4f42dd40a07038d5e956a551de5f (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    /*

    Copyright (C) 2001-2002 Stefan Westerfeld
                            stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#include "thread.h"
#include "debug.h"
#include "gslglib.h"
#include <pthread.h>

#if defined(__DECCXX)
#define EXTC extern "C"
#else
#define EXTC
#endif

EXTC gpointer
gsl_arts_mutex_new ()
{
  return new Arts::Mutex;
}

EXTC void
gsl_arts_mutex_free (gpointer mutex)
{
  Arts::Mutex *m = static_cast<Arts::Mutex *>(mutex);
  delete m;
}

EXTC void
gsl_arts_mutex_lock (gpointer mutex)
{
  Arts::Mutex *m = static_cast<Arts::Mutex *>(mutex);
  m->lock();
}

EXTC gboolean
gsl_arts_mutex_trylock (gpointer mutex)
{
  Arts::Mutex *m = static_cast<Arts::Mutex *>(mutex);
  return m->tryLock();
}

EXTC void
gsl_arts_mutex_unlock (gpointer mutex)
{
  Arts::Mutex *m = static_cast<Arts::Mutex *>(mutex);
  m->unlock();
}

EXTC gpointer
gsl_arts_cond_new ()
{
  return new Arts::ThreadCondition;
}

EXTC void
gsl_arts_cond_free (gpointer cond)
{
  Arts::ThreadCondition *c = static_cast<Arts::ThreadCondition *>(cond);
  delete c;
}

EXTC void
gsl_arts_cond_signal (gpointer cond)
{
  Arts::ThreadCondition *c = static_cast<Arts::ThreadCondition *>(cond);
  c->wakeOne();
}

EXTC void
gsl_arts_cond_broadcast (gpointer cond)
{
  Arts::ThreadCondition *c = static_cast<Arts::ThreadCondition *>(cond);
  c->wakeAll();
}

EXTC void
gsl_arts_cond_wait (gpointer cond, gpointer mutex)
{
  Arts::ThreadCondition *c = static_cast<Arts::ThreadCondition *>(cond);
  Arts::Mutex *m = static_cast<Arts::Mutex *>(mutex);

  c->wait(*m);
}

EXTC void
gsl_arts_cond_timed_wait (gpointer /*cond*/, gpointer /*mutex*/, GTimeVal * /*time*/)
{
  arts_assert(false);
}


class GslArtsThread : public Arts::Thread {
protected:
  gpointer (*func)(gpointer data);
  gpointer data;
  gpointer result;

public:
  GThread gthread;

  GslArtsThread(gpointer (*func)(gpointer data), gpointer data)
    : func(func), data(data)
  {
    gthread.data = 0;
  }
  void run()
  {
    result = func(data);
  }
};


/* KCC (KAI C++) is buggy.  If we write out the type of the first argument
   to gsl_arts_thread_create(), ala
     gsl_arts_thread_create (gpointer (*func) (gpointer data2), ...)
   it becomes C++ linkage, i.e. it's name gets mangled, _despite_ declared
   extern "C" in the header.  Other sources only calling this function,
   i.e. those only seeing the prototype correctly call the unmangled
   extern "C" variant, but for the above reason it isn't defined anywhere.
   The solution is to go through a typedef for that argument, _which must
   also be declared extern "C"_.  I'm not sure, but I think it's an error
   of KCC, that it puts the invisible type of the first arg into C++ mode,
   although the whole function should be C only.  If one declares
   two equal function types, one extern "C", one not, they are even assignment
   compatible.  But somehow KCC puts that type into C++ mode, which for
   other strange reasons force the whole function to go into C++ linkage.
   It's enough, when this typedef is local to this file.  (matz)  */
   
/* Due to gcc's unhappyness with 'extern "C" typedef ...' we enclose
   it in a real extern "C" {} block.  */
extern "C" {
typedef gpointer (*t_func)(gpointer data2);
}

// This is C++:
GThread* gsl_arts_thread_create_full_CPP(gpointer (*func)(gpointer data),
                                  gpointer               data,
                                  gulong                 /*stack_size*/,
                                  gboolean               /*joinable*/,
                                  gboolean               /*bound*/,
                                  GThreadPriority        /*priority*/,
                                  GError               ** /*error*/) 
{
  GslArtsThread *thread = new GslArtsThread(func, data);
  return &thread->gthread;
}

// This is C:
extern "C" GThread* 
gsl_arts_thread_create_full(gpointer (*func)(gpointer data),
                                  gpointer               data,
                                  gulong                 stack_size,
                                  gboolean               joinable,
                                  gboolean               bound,
                                  GThreadPriority        priority,
                                  GError               **error)
{
  return gsl_arts_thread_create_full_CPP(func, 
                                         data, 
					 stack_size, 
					 joinable, 
					 bound,
					 priority,
					 error);
}

EXTC gpointer
gsl_arts_thread_self ()
{
  GslArtsThread *current = static_cast<GslArtsThread *>(Arts::SystemThreads::the()->getCurrentThread());
 
  if(current)
    return &current->gthread;
  else
    {
      static GThread mainThread = { 0, };
      return &mainThread;
    }
}

EXTC void 
gsl_arts_thread_init (gpointer /*arg*/)
{
}