2013-08-31 13:46:42 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
|
|
|
|
|
2016-02-28 07:03:24 -05:00
|
|
|
This file is part of the Sortix C Library.
|
2013-08-31 13:46:42 -04:00
|
|
|
|
2016-02-28 07:03:24 -05:00
|
|
|
The Sortix C Library is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or (at your
|
2013-08-31 13:46:42 -04:00
|
|
|
option) any later version.
|
|
|
|
|
2016-02-28 07:03:24 -05:00
|
|
|
The Sortix C Library is distributed in the hope that it will be useful, but
|
2013-08-31 13:46:42 -04:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2016-02-28 07:03:24 -05:00
|
|
|
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
2013-08-31 13:46:42 -04:00
|
|
|
|
2016-02-28 07:03:24 -05:00
|
|
|
pthread/pthread_exit.c
|
2013-08-31 13:46:42 -04:00
|
|
|
Exits the current thread.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2016-02-28 07:03:24 -05:00
|
|
|
#include <stdbool.h>
|
2013-08-31 13:46:42 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
__attribute__((__noreturn__))
|
2014-01-09 17:20:45 -05:00
|
|
|
void pthread_exit(void* return_value)
|
2013-08-31 13:46:42 -04:00
|
|
|
{
|
|
|
|
struct pthread* thread = pthread_self();
|
2014-01-13 16:33:00 -05:00
|
|
|
|
|
|
|
pthread_mutex_lock(&__pthread_keys_lock);
|
|
|
|
bool keys_left = true;
|
|
|
|
while ( keys_left )
|
|
|
|
{
|
|
|
|
keys_left = false;
|
|
|
|
for ( pthread_key_t key = 0; key < thread->keys_length; key++ )
|
|
|
|
{
|
|
|
|
void* key_value = thread->keys[key];
|
|
|
|
if ( !key_value )
|
|
|
|
continue;
|
|
|
|
thread->keys[key] = NULL;
|
|
|
|
if ( __pthread_keys[key].destructor )
|
|
|
|
__pthread_keys[key].destructor(key_value);
|
|
|
|
keys_left = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(thread->keys);
|
|
|
|
thread->keys = NULL;
|
|
|
|
thread->keys_length = 0;
|
|
|
|
pthread_mutex_unlock(&__pthread_keys_lock);
|
|
|
|
|
2014-01-10 09:43:16 -05:00
|
|
|
pthread_mutex_lock(&thread->detach_lock);
|
2014-01-09 17:20:45 -05:00
|
|
|
thread->exit_result = return_value;
|
2013-08-04 14:24:59 -04:00
|
|
|
int exit_flags = EXIT_THREAD_UNMAP;
|
2013-08-31 13:46:42 -04:00
|
|
|
struct exit_thread extended;
|
|
|
|
memset(&extended, 0, sizeof(extended));
|
|
|
|
extended.unmap_from = thread->uthread.stack_mmap;
|
|
|
|
extended.unmap_size = thread->uthread.stack_size;
|
2014-01-09 17:48:43 -05:00
|
|
|
if ( thread->detach_state == PTHREAD_CREATE_JOINABLE )
|
|
|
|
{
|
|
|
|
extended.zero_from = &thread->join_lock.lock;
|
|
|
|
extended.zero_size = sizeof(thread->join_lock.lock);
|
2013-08-04 14:24:59 -04:00
|
|
|
exit_flags |= EXIT_THREAD_ZERO;
|
2014-01-09 17:48:43 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-04 14:24:59 -04:00
|
|
|
extended.tls_unmap_from = thread->uthread.tls_mmap;
|
|
|
|
extended.tls_unmap_size = thread->uthread.tls_size;
|
|
|
|
exit_flags |= EXIT_THREAD_TLS_UNMAP;
|
2014-01-09 17:48:43 -05:00
|
|
|
}
|
2013-08-04 14:24:59 -04:00
|
|
|
|
|
|
|
exit_thread(0, EXIT_THREAD_ONLY_IF_OTHERS | exit_flags, &extended);
|
|
|
|
exit(0);
|
2013-08-31 13:46:42 -04:00
|
|
|
}
|