all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 337134fe7475b01551ae6cb2ffff57276bad1008 9755 bytes (raw)
name: src/thread.c 	 # note: path name is non-authoritative(*)

  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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
 
/* Threading code.
   Copyright (C) 2011 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GNU Emacs 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */


#include <config.h>
#include <setjmp.h>
#include "lisp.h"
#include "buffer.h"
#include "blockinput.h"
#include <pthread.h>
#include <sched.h>
#include "systime.h"
#include "sysselect.h"

void mark_catchlist (struct catchtag *);
void mark_stack (char *, char *);
void flush_stack_call_func (void (*) (char *, void *), void *);

Lisp_Object Qthreadp;

/* The main thread that exists when Emacs starts.  */
static struct thread_state primary_thread;

/* A linked list of all threads in existence.  */
static struct thread_state *all_threads = &primary_thread;

/* The Lisp thread object for the current thread.  Note that this is
   thread-local.  */
__thread struct thread_state *current_thread = &primary_thread;

/* Only one thread can run Lisp code at a time.  This thread holds the
   global lock.  */
static pthread_mutex_t global_lock;

static void
mark_one_thread (struct thread_state *thread)
{
  struct specbinding *bind;
  struct handler *handler;
  Lisp_Object tem;

  for (bind = thread->m_specpdl; bind != thread->m_specpdl_ptr; bind++)
    {
      mark_object (bind->symbol);
      mark_object (bind->old_value);
    }

#if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
     || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
  mark_stack (thread->stack_bottom, thread->stack_top);
#else
  {
    struct gcpro *tail;
    for (tail = thread->m_gcprolist; tail; tail = tail->next)
      for (i = 0; i < tail->nvars; i++)
	mark_object (tail->var[i]);
  }
#endif

#if BYTE_MARK_STACK
  if (thread->m_byte_stack_list)
    mark_byte_stack (thread->m_byte_stack_list);
#endif

  mark_catchlist (thread->m_catchlist);

  for (handler = thread->m_handlerlist; handler; handler = handler->next)
    {
      mark_object (handler->handler);
      mark_object (handler->var);
    }

#if BYTE_MARK_STACK
  mark_backtrace (thread->m_backtrace_list);
#endif

  if (thread->m_current_buffer)
    {
      XSETBUFFER (tem, thread->m_current_buffer);
      mark_object (tem);
    }

  mark_object (thread->m_last_thing_searched);

  if (thread->m_saved_last_thing_searched)
    mark_object (thread->m_saved_last_thing_searched);
}

static void
mark_threads_callback (char *end, void *ignore)
{
  struct thread_state *iter;

  current_thread->stack_top = end;
  for (iter = all_threads; iter; iter = iter->next_thread)
    {
      Lisp_Object thread_obj;
      XSETTHREAD (thread_obj, iter);
      mark_object (thread_obj);
      mark_one_thread (iter);
    }
}

void
mark_threads (void)
{
  flush_stack_call_func (mark_threads_callback, NULL);
}

void
unmark_threads (void)
{
  struct thread_state *iter;

  for (iter = all_threads; iter; iter = iter->next_thread)
    if (iter->m_byte_stack_list)
      unmark_byte_stack (iter->m_byte_stack_list);
}

static void
thread_yield_callback (char *end, void *ignore)
{
  pthread_mutex_unlock (&global_lock);
  sched_yield ();
  pthread_mutex_lock (&global_lock);
}

void
thread_yield (void)
{
  flush_stack_call_func (thread_yield_callback, NULL);
}

DEFUN ("thread-yield", Fthread_yield, Sthread_yield, 0, 0, 0,
       doc: /* Yield to the next thread.  */)
     (void)
{
  thread_yield ();
  return other_threads_p () ? Qt : Qnil;
}

static Lisp_Object
invoke_thread_function (void)
{
  Lisp_Object iter;

  int count = SPECPDL_INDEX ();

  Ffuncall (1, &current_thread->func);
  return unbind_to (count, Qnil);
}

static Lisp_Object
do_nothing (Lisp_Object whatever)
{
  return whatever;
}

static void *
run_thread (void *state)
{
  struct thread_state *self = state;
  struct thread_state **iter;
  struct gcpro gcpro1;
  Lisp_Object buffer;
  char stack_pos;

  self->stack_top = self->stack_bottom = &stack_pos;

  self->m_specpdl_size = 50;
  self->m_specpdl = xmalloc (self->m_specpdl_size
			     * sizeof (struct specbinding));
  self->m_specpdl_ptr = self->m_specpdl;
  self->pthread_id = pthread_self ();

  /* Thread-local assignment.  */
  current_thread = self;

  /* We need special handling to set the initial buffer.  Our parent
     thread is very likely to be using this same buffer so we will
     typically wait for the parent thread to release it first.  */
  XSETBUFFER (buffer, self->m_current_buffer);
  GCPRO1 (buffer);
  self->m_current_buffer = 0;

  pthread_mutex_lock (&global_lock);

  set_buffer_internal (XBUFFER (buffer));

  /* It might be nice to do something with errors here.  */
  internal_condition_case (invoke_thread_function, Qt, do_nothing);

  {
    /*FIXME*/
    extern void blocal_unbind_thread (Lisp_Object);
    blocal_unbind_thread (Fcurrent_thread ());
  }

  /* Unlink this thread from the list of all threads.  */
  for (iter = &all_threads; *iter != self; iter = &(*iter)->next_thread)
    ;
  *iter = (*iter)->next_thread;

  xfree (self->m_specpdl);

  pthread_mutex_unlock (&global_lock);

  return NULL;
}

DEFUN ("make-thread", Fmake_thread, Smake_thread, 1, 2, 0,
       doc: /* Start a new thread and run FUNCTION in it.
When the function exits, the thread dies.
NAME is the name of the thread; it defaults to nil.  */)
  (Lisp_Object function, Lisp_Object name)
{
  char stack_pos;
  pthread_t thr;
  struct thread_state *new_thread;
  struct specbinding *p;

  /* Can't start a thread in temacs.  */
  if (!initialized)
    abort ();

  new_thread = ALLOCATE_PSEUDOVECTOR (struct thread_state, m_gcprolist,
				      PVEC_THREAD);
  memset ((char *) new_thread + offsetof (struct thread_state, m_gcprolist),
	  0, sizeof (struct thread_state) - offsetof (struct thread_state,
						      m_gcprolist));

  new_thread->func = function;
  new_thread->name = name;
  new_thread->m_last_thing_searched = Qnil; /* copy from parent? */
  new_thread->m_saved_last_thing_searched = Qnil;
  new_thread->m_current_buffer = current_thread->m_current_buffer;
  new_thread->stack_bottom = &stack_pos;

  initialize_globals (&new_thread->g);

  /* We'll need locking here.  */
  new_thread->next_thread = all_threads;
  all_threads = new_thread;

  if (pthread_create (&thr, NULL, run_thread, new_thread))
    {
      /* Restore the previous situation.  */
      all_threads = all_threads->next_thread;
      error ("Could not start a new thread");
    }

  return Qnil;
}

DEFUN ("current-thread", Fcurrent_thread, Scurrent_thread, 0, 0, 0,
       doc: /* Return the current thread.  */)
  (void)
{
  Lisp_Object result;
  XSETTHREAD (result, current_thread);
  return result;
}

DEFUN ("thread-name", Fthread_name, Sthread_name, 1, 1, 0,
       doc: /* Return the name of the THREAD.
The name is the same object that was passed to `make-thread'.  */)
     (Lisp_Object thread)
{
  struct thread_state *tstate;

  CHECK_THREAD (thread);
  tstate = XTHREAD (thread);

  return tstate->name;
}

DEFUN ("all-threads", Fall_threads, Sall_threads, 0, 0, 0,
       doc: /* Return a list of all threads.  */)
     (void)
{
  Lisp_Object result = Qnil;
  struct thread_state *iter;

  for (iter = all_threads; iter; iter = iter->next_thread)
    {
      Lisp_Object thread;

      XSETTHREAD (thread, iter);
      result = Fcons (thread, result);
    }

  return result;
}

/* Get the main thread as a lisp object.  */
Lisp_Object
get_main_thread (void)
{
  Lisp_Object result;
  XSETTHREAD (result, &primary_thread);
  return result;
}

/* Is the current an user thread.  */
int
user_thread_p (void)
{
  struct thread_state *it = all_threads;
  pthread_t self = pthread_self ();
  do
    {
      if (it->pthread_id == self)
	return 1;
    }
  while (it = it->next_thread);

  return 0;
}

int
thread_select (int n, SELECT_TYPE *rfd, SELECT_TYPE *wfd, SELECT_TYPE *xfd,
	       EMACS_TIME *tmo)
{
  char end;
  int ret;

  /* FIXME: must call flush_stack_call_func */
  pthread_mutex_unlock (&global_lock);

  ret = select (n, rfd, wfd, xfd, tmo);

  pthread_mutex_lock (&global_lock);

  return ret;
}

int
other_threads_p (void)
{
  return all_threads->header.next.vector ? 1 : 0;
}

Lisp_Object
thread_notify_kill_buffer (struct buffer *b)
{
  Lisp_Object tem;
  struct thread_state *it = all_threads;
  for (; it; it = it->next_thread)
    {
      if (b == it->m_current_buffer)
        {
	  Lisp_Object buf;
          XSETBUFFER (buf, it->m_current_buffer);
          tem = Fother_buffer (buf, Qnil, Qnil);
          it->m_current_buffer = XBUFFER (tem);
          if (b == it->m_current_buffer)
            return Qnil;
        }
    }

  return Qt;
}

void
init_threads_once (void)
{
  primary_thread.header.size = PSEUDOVECSIZE (struct thread_state, m_gcprolist);
  primary_thread.header.next.vector = NULL;
  primary_thread.func = Qnil;
  primary_thread.name = Qnil;
  XSETPVECTYPE (&primary_thread, PVEC_THREAD);
}

void
init_threads (void)
{
  pthread_mutex_init (&global_lock, NULL);
  pthread_mutex_lock (&global_lock);

  primary_thread.pthread_id = pthread_self ();
  primary_thread.m_last_thing_searched = Qnil;
  all_threads = &primary_thread;
}

void
syms_of_threads (void)
{
  defsubr (&Smake_thread);
  defsubr (&Sthread_yield);
  defsubr (&Scurrent_thread);
  defsubr (&Sthread_name);
  defsubr (&Sall_threads);

  DEFSYM (Qthreadp, "threadp");
}

debug log:

solving 337134f ...
found 337134f in https://yhetil.org/emacs/87obntotn2.fsf@fleche.redhat.com/

applying [1/1] https://yhetil.org/emacs/87obntotn2.fsf@fleche.redhat.com/
diff --git a/src/thread.c b/src/thread.c
new file mode 100644
index 0000000..337134f

Checking patch src/thread.c...
Applied patch src/thread.c cleanly.

index at:
100644 337134fe7475b01551ae6cb2ffff57276bad1008	src/thread.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.