summaryrefslogtreecommitdiff
path: root/libcult/cult/sched/thread.cxx
blob: 89368b6f9043994dc31a7c022776ab6825b1e21a (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
204
205
206
207
208
209
210
211
// file      : cult/sched/thread.cxx
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <cult/sched/thread.hxx>
#include <cult/sched/lock.hxx>
#include <cult/sched/exception.hxx>

#include <cult/mm/counter.hxx> // MM::inc_ref

#include <cult/trace/stream.hxx>

namespace Cult
{
  namespace Sched
  {
    namespace
    {
      Trace::Stream tout ("Cult::Sched::Thread", 7);
    }

    namespace Bits
    {
      typedef Void* (*Routine) (Void*);

      struct StartData
      {
        StartData (Shptr<Thread> const& thread, Routine routine, void* arg)
            : thread_ (thread), routine_ (routine), arg_ (arg)
        {
        }

        ~StartData ()
        {
          tout << 8 << "start data is being destroyed.";
        }

        Shptr<Thread> thread_;
        Routine routine_;
        Void* arg_;
      };

      static pthread_key_t key;
      static pthread_once_t key_once = PTHREAD_ONCE_INIT;

      extern "C" Void
      cult_thread_dtor (Void* p)
      {
        // Exception in this function will result in the call
        // to std::terminate().
        //

        tout << "cult_thread_dtor is being executed.";

        Shptr<Thread> self (reinterpret_cast<Thread*> (p));
      }

      extern "C" Void
      cult_thread_make_key ()
      {
        if (Int e = pthread_key_create (&key, &cult_thread_dtor))
          throw Implementation (e);
      }

      extern "C" Void*
      cult_thread_trampoline (Void* arg)
      {
        // Any failure in this function will result in the call
        // to std::terminate().
        //

        Routine routine;

        {
          Shptr<StartData> data (reinterpret_cast<StartData*> (arg));

          Thread* p (data->thread_.get ());

          if (Int e = pthread_setspecific (key, p))
            throw Implementation (e);
          else
            MM::inc_ref (p);

          routine = data->routine_;
          arg = data->arg_;
        }

        return routine (arg);
      }
    }

    Thread::
    Thread (Void* (*routine) (Void*), Void* arg)
        : detached_ (false)
    {
      using Bits::StartData;

      tout << "thread is being constructed.";

      pthread_once (&Bits::key_once, &Bits::cult_thread_make_key);

      Shptr<Thread> self (MM::inc_ref (this));

      Shptr<StartData> data (new StartData (self, routine, arg));

      if (Int e = pthread_create (&id_,
                                  0,
                                  &Bits::cult_thread_trampoline,
                                  data.get ()))
      {
        throw Implementation (e);
      }
      else
      {
        // If pthread_create did not fail then thread_trampoline
        // will release the data.
        //
        data.release ();
      }
    }

    Thread::
    Thread ()
        : id_ (pthread_self ()), detached_ (false) //@@ We can't be sure
                                                   //   the it is detached.
    {
      tout << "thread is being adopted.";

      pthread_once (&Bits::key_once, &Bits::cult_thread_make_key);

      if (pthread_getspecific (Bits::key) != 0)
        throw Adopted ();

      Shptr<Thread> self (MM::inc_ref (this));

      if(Int e = pthread_setspecific (Bits::key, this))
      {
        throw Implementation (e);
      }
      else
      {
        // TSD slot has the reference now.
        //
        self.release ();
      }
    }

    Void* Thread::
    join ()
    {
      Lock lock (mutex_);

      if (detached_)
        throw Joined ();

      Void* r;

      if (Int e = pthread_join (id_, &r))
        throw Implementation (e);

      detached_ = true;

      return r;
    }

    Void Thread::
    cancel ()
    {
      if (Int e = pthread_cancel (id_))
        throw Implementation (e);
    }

    Void Thread::
    exit (Void* ret)
    {
      pthread_exit (ret);
    }

    Shptr<Thread> Thread::
    self  ()
    {
      Thread* p (reinterpret_cast<Thread*> (pthread_getspecific (Bits::key)));

      if (p != 0)
        return Shptr<Thread> (MM::inc_ref (p));
      else
        throw Foreign ();
    }

    Void Thread::
    test_cancel ()
    {
      pthread_testcancel ();
    }

    Thread::
    ~Thread ()
    {
      tout << "thread is being destroyed.";

      Lock lock (mutex_);

      if (!detached_)
      {
        if (Int e = pthread_detach (id_))
          throw Implementation (e);
      }
    }
  }
}