416b17447526ca8045a5afa530ce9ad4f095bbb9
[nit.git] / lib / pthreads / extra.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Offers some POSIX threads services that are not available on all platforms
18 module extra is
19 c_compiler_option("-pthread")
20 c_linker_option("-pthread")
21 end
22
23 intrude import pthreads
24
25 in "C" `{
26 // TODO protect with: #ifdef WITH_LIBGC
27 #ifndef ANDROID
28 #define GC_THREADS
29 #include <gc.h>
30 #endif
31 `}
32
33 redef extern class NativePthread
34 fun cancel: Bool `{
35 return pthread_cancel(*recv);
36 `}
37 end
38
39 redef class Thread
40 # Cancel the execution of the thread
41 fun cancel
42 do
43 if native == null then return
44 native.cancel
45 native = null
46 end
47 end
48
49 # Does not return if the running thread is to be cancelled
50 fun test_cancel `{ pthread_testcancel(); `}
51
52 private extern class NativePthreadBarrier in "C" `{ pthread_barrier_t * `}
53 new(count: Int) `{
54 pthread_barrier_t *barrier = malloc(sizeof(pthread_barrier_t));
55 int res = pthread_barrier_init(barrier, NULL, count);
56 return barrier;
57 `}
58
59 fun destroy `{ pthread_barrier_destroy(recv); `}
60
61 fun wait `{ pthread_barrier_wait(recv); `}
62 end