compat / win32 / pthread.con commit Merge branch 'es/doc-gitsubmodules-markup' (c41b119)
   1/*
   2 * Copyright (C) 2009 Andrzej K. Haczewski <ahaczewski@gmail.com>
   3 *
   4 * DISCLAIMER: The implementation is Git-specific, it is subset of original
   5 * Pthreads API, without lots of other features that Git doesn't use.
   6 * Git also makes sure that the passed arguments are valid, so there's
   7 * no need for double-checking.
   8 */
   9
  10#include "../../git-compat-util.h"
  11#include "pthread.h"
  12
  13#include <errno.h>
  14#include <limits.h>
  15
  16static unsigned __stdcall win32_start_routine(void *arg)
  17{
  18        pthread_t *thread = arg;
  19        thread->tid = GetCurrentThreadId();
  20        thread->arg = thread->start_routine(thread->arg);
  21        return 0;
  22}
  23
  24int pthread_create(pthread_t *thread, const void *unused,
  25                   void *(*start_routine)(void*), void *arg)
  26{
  27        thread->arg = arg;
  28        thread->start_routine = start_routine;
  29        thread->handle = (HANDLE)
  30                _beginthreadex(NULL, 0, win32_start_routine, thread, 0, NULL);
  31
  32        if (!thread->handle)
  33                return errno;
  34        else
  35                return 0;
  36}
  37
  38int win32_pthread_join(pthread_t *thread, void **value_ptr)
  39{
  40        DWORD result = WaitForSingleObject(thread->handle, INFINITE);
  41        switch (result) {
  42                case WAIT_OBJECT_0:
  43                        if (value_ptr)
  44                                *value_ptr = thread->arg;
  45                        return 0;
  46                case WAIT_ABANDONED:
  47                        return EINVAL;
  48                default:
  49                        return err_win_to_posix(GetLastError());
  50        }
  51}
  52
  53pthread_t pthread_self(void)
  54{
  55        pthread_t t = { NULL };
  56        t.tid = GetCurrentThreadId();
  57        return t;
  58}