src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 25 Feb 2018 19:51:34 -0800
changeset 11905 29bf3038a246
parent 11811 5d94cb6b24d3
child 12503 806492103856
permissions -rw-r--r--
Fixed bug 4097 - Segmentation fault by SDL_CreateThreadWithStackSize

Dongsun Kim

Normal case
1. [thread 1] SDL_CreateThreadWithStackSize calls SDL_SYS_CreateThread.
2. [thread 1] If successful, it calls SDL_SemWait.
3. [thread 2] SDL_RunThread calls SDL_SYS_SetupThread, SDL_ThreadID, SDL_SemPost.
4. [thread 1] SDL_CreateThreadWithStackSize calls SDL_DestroySemaphore, SDL_free.

Crash case (Segmentation fault)
1. [thread 1] SDL_CreateThreadWithStackSize calls SDL_SYS_CreateThread.
2. [thread 1] If successful, it calls SDL_SemWait.
--> Error return due to SIGNAL(SYSTEM or Real Time) at sem_wait(pthread).
3. [thread 1] SDL_CreateThreadWithStackSize calls SDL_DestroySemaphore, SDL_free.
4. [thread 2] SDL_RunThread calls SDL_SYS_SetupThread, SDL_ThreadID, SDL_SemPost.
--> Segmentation fault at strlen or sem_post.
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@1361
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1361
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1361
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1361
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1361
    22
slouken@5106
    23
#include <errno.h>
slouken@1361
    24
#include <pthread.h>
slouken@1361
    25
#include <semaphore.h>
icculus@5981
    26
#include <sys/time.h>
slouken@9244
    27
#include <time.h>
slouken@1361
    28
slouken@1361
    29
#include "SDL_thread.h"
slouken@1361
    30
#include "SDL_timer.h"
slouken@1361
    31
slouken@1361
    32
/* Wrapper around POSIX 1003.1b semaphores */
slouken@1361
    33
slouken@2765
    34
#if defined(__MACOSX__) || defined(__IPHONEOS__)
slouken@1627
    35
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
slouken@1627
    36
#include "../generic/SDL_syssem.c"
slouken@1627
    37
#else
slouken@1361
    38
slouken@1895
    39
struct SDL_semaphore
slouken@1895
    40
{
slouken@1895
    41
    sem_t sem;
slouken@1361
    42
};
slouken@1361
    43
slouken@1361
    44
/* Create a semaphore, initialized with value */
slouken@1895
    45
SDL_sem *
slouken@1895
    46
SDL_CreateSemaphore(Uint32 initial_value)
slouken@1361
    47
{
slouken@1895
    48
    SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
slouken@1895
    49
    if (sem) {
slouken@1895
    50
        if (sem_init(&sem->sem, 0, initial_value) < 0) {
slouken@1895
    51
            SDL_SetError("sem_init() failed");
slouken@1895
    52
            SDL_free(sem);
slouken@1895
    53
            sem = NULL;
slouken@1895
    54
        }
slouken@1895
    55
    } else {
slouken@1895
    56
        SDL_OutOfMemory();
slouken@1895
    57
    }
slouken@1895
    58
    return sem;
slouken@1361
    59
}
slouken@1361
    60
slouken@1895
    61
void
slouken@1895
    62
SDL_DestroySemaphore(SDL_sem * sem)
slouken@1361
    63
{
slouken@1895
    64
    if (sem) {
slouken@1895
    65
        sem_destroy(&sem->sem);
slouken@1895
    66
        SDL_free(sem);
slouken@1895
    67
    }
slouken@1361
    68
}
slouken@1361
    69
slouken@1895
    70
int
slouken@1895
    71
SDL_SemTryWait(SDL_sem * sem)
slouken@1361
    72
{
slouken@1895
    73
    int retval;
slouken@1361
    74
slouken@1895
    75
    if (!sem) {
icculus@7037
    76
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
    77
    }
slouken@1895
    78
    retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
    79
    if (sem_trywait(&sem->sem) == 0) {
slouken@1895
    80
        retval = 0;
slouken@1895
    81
    }
slouken@1895
    82
    return retval;
slouken@1361
    83
}
slouken@1361
    84
slouken@1895
    85
int
slouken@1895
    86
SDL_SemWait(SDL_sem * sem)
slouken@1361
    87
{
slouken@1895
    88
    int retval;
slouken@1361
    89
slouken@1895
    90
    if (!sem) {
icculus@7037
    91
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
    92
    }
slouken@1361
    93
slouken@11905
    94
    do {
slouken@11905
    95
        retval = sem_wait(&sem->sem);
slouken@11905
    96
    } while (retval < 0 && errno == EINTR);
slouken@11905
    97
slouken@1895
    98
    if (retval < 0) {
icculus@7037
    99
        retval = SDL_SetError("sem_wait() failed");
slouken@1895
   100
    }
slouken@1895
   101
    return retval;
slouken@1361
   102
}
slouken@1361
   103
slouken@1895
   104
int
slouken@1895
   105
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@1361
   106
{
slouken@1895
   107
    int retval;
slouken@6219
   108
#ifdef HAVE_SEM_TIMEDWAIT
slouken@9244
   109
#ifndef HAVE_CLOCK_GETTIME
slouken@5106
   110
    struct timeval now;
slouken@9244
   111
#endif
slouken@5106
   112
    struct timespec ts_timeout;
slouken@6219
   113
#else
slouken@6219
   114
    Uint32 end;
slouken@6219
   115
#endif
slouken@1361
   116
slouken@1895
   117
    if (!sem) {
icculus@7037
   118
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
   119
    }
slouken@1361
   120
slouken@1895
   121
    /* Try the easy cases first */
slouken@1895
   122
    if (timeout == 0) {
slouken@1895
   123
        return SDL_SemTryWait(sem);
slouken@1895
   124
    }
slouken@1895
   125
    if (timeout == SDL_MUTEX_MAXWAIT) {
slouken@1895
   126
        return SDL_SemWait(sem);
slouken@1895
   127
    }
slouken@1361
   128
slouken@6219
   129
#ifdef HAVE_SEM_TIMEDWAIT
slouken@5106
   130
    /* Setup the timeout. sem_timedwait doesn't wait for
slouken@5106
   131
    * a lapse of time, but until we reach a certain time.
slouken@5106
   132
    * This time is now plus the timeout.
slouken@5106
   133
    */
slouken@9244
   134
#ifdef HAVE_CLOCK_GETTIME
slouken@9244
   135
    clock_gettime(CLOCK_REALTIME, &ts_timeout);
slouken@9244
   136
slouken@9244
   137
    /* Add our timeout to current time */
slouken@9244
   138
    ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
slouken@9244
   139
    ts_timeout.tv_sec += timeout / 1000;
slouken@9244
   140
#else
slouken@5106
   141
    gettimeofday(&now, NULL);
slouken@5106
   142
slouken@5106
   143
    /* Add our timeout to current time */
slouken@9244
   144
    ts_timeout.tv_sec = now.tv_sec + (timeout / 1000);
slouken@9244
   145
    ts_timeout.tv_nsec = (now.tv_usec + (timeout % 1000) * 1000) * 1000;
slouken@9244
   146
#endif
slouken@5106
   147
slouken@5106
   148
    /* Wrap the second if needed */
slouken@9244
   149
    if (ts_timeout.tv_nsec > 1000000000) {
slouken@9244
   150
        ts_timeout.tv_sec += 1;
slouken@9244
   151
        ts_timeout.tv_nsec -= 1000000000;
slouken@5106
   152
    }
slouken@5106
   153
slouken@5106
   154
    /* Wait. */
slouken@1895
   155
    do {
slouken@5106
   156
        retval = sem_timedwait(&sem->sem, &ts_timeout);
slouken@5106
   157
    } while (retval < 0 && errno == EINTR);
slouken@5106
   158
slouken@5106
   159
    if (retval < 0) {
slouken@6298
   160
        if (errno == ETIMEDOUT) {
slouken@6298
   161
            retval = SDL_MUTEX_TIMEDOUT;
slouken@6298
   162
        } else {
brandon@8831
   163
            SDL_SetError("sem_timedwait returned an error: %s", strerror(errno));
slouken@6298
   164
        }
slouken@5106
   165
    }
slouken@6219
   166
#else
slouken@6219
   167
    end = SDL_GetTicks() + timeout;
slouken@6219
   168
    while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
slouken@7857
   169
        if (SDL_TICKS_PASSED(SDL_GetTicks(), end)) {
slouken@6219
   170
            break;
slouken@6219
   171
        }
slouken@7732
   172
        SDL_Delay(1);
slouken@6219
   173
    }
slouken@6219
   174
#endif /* HAVE_SEM_TIMEDWAIT */
slouken@1361
   175
slouken@1895
   176
    return retval;
slouken@1361
   177
}
slouken@1361
   178
slouken@1895
   179
Uint32
slouken@1895
   180
SDL_SemValue(SDL_sem * sem)
slouken@1361
   181
{
slouken@1895
   182
    int ret = 0;
slouken@1895
   183
    if (sem) {
slouken@1895
   184
        sem_getvalue(&sem->sem, &ret);
slouken@1895
   185
        if (ret < 0) {
slouken@1895
   186
            ret = 0;
slouken@1895
   187
        }
slouken@1895
   188
    }
slouken@1895
   189
    return (Uint32) ret;
slouken@1361
   190
}
slouken@1361
   191
slouken@1895
   192
int
slouken@1895
   193
SDL_SemPost(SDL_sem * sem)
slouken@1361
   194
{
slouken@1895
   195
    int retval;
slouken@1361
   196
slouken@1895
   197
    if (!sem) {
icculus@7037
   198
        return SDL_SetError("Passed a NULL semaphore");
slouken@1895
   199
    }
slouken@1361
   200
slouken@1895
   201
    retval = sem_post(&sem->sem);
slouken@1895
   202
    if (retval < 0) {
slouken@1895
   203
        SDL_SetError("sem_post() failed");
slouken@1895
   204
    }
slouken@1895
   205
    return retval;
slouken@1361
   206
}
slouken@1627
   207
slouken@1627
   208
#endif /* __MACOSX__ */
slouken@1895
   209
/* vi: set ts=4 sw=4 expandtab: */