pthread.c 12.6 KB
Newer Older
Y
yiyue.fang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * File      : pthread.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2012, RT-Thread Development Team
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 */

M
Ming, Bai 已提交
24 25 26 27 28 29
#include <pthread.h>
#include <sched.h>
#include "pthread_internal.h"

int pthread_system_init(void)
{
Y
yiyue.fang 已提交
30 31
    /* initialize clock and time */
    clock_time_system_init();
M
Ming, Bai 已提交
32

Y
yiyue.fang 已提交
33 34 35 36 37 38
    /* initialize key area */
    pthread_key_system_init();
    /* initialize posix mqueue */
    posix_mq_system_init();
    /* initialize posix semaphore */
    posix_sem_system_init();
M
Ming, Bai 已提交
39

Y
yiyue.fang 已提交
40
    return 0;
M
Ming, Bai 已提交
41
}
42
INIT_COMPONENT_EXPORT(pthread_system_init);
M
Ming, Bai 已提交
43 44 45

static void _pthread_cleanup(rt_thread_t tid)
{
Y
yiyue.fang 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
    _pthread_data_t *ptd;
    ptd = _pthread_get_data(tid);

    /* clear cleanup function */
    tid->cleanup = RT_NULL;
    if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
    {
        rt_sem_release(ptd->joinable_sem);
    }
    else
    {
        /* release pthread resource */
        pthread_detach(tid);
    }
M
Ming, Bai 已提交
60 61
}

Y
yiyue.fang 已提交
62
static void pthread_entry_stub(void *parameter)
M
Ming, Bai 已提交
63
{
Y
yiyue.fang 已提交
64 65
    _pthread_data_t *ptd;
    void *value;
M
Ming, Bai 已提交
66

Y
yiyue.fang 已提交
67
    ptd = (_pthread_data_t*)parameter;
M
Ming, Bai 已提交
68

Y
yiyue.fang 已提交
69 70 71 72
    /* execute pthread entry */
    value = ptd->thread_entry(ptd->thread_parameter);
    /* set value */
    ptd->return_value = value;
M
Ming, Bai 已提交
73 74
}

Y
yiyue.fang 已提交
75 76 77
int pthread_create(pthread_t            *tid,
                   const pthread_attr_t *attr, 
                   void *(*start) (void *), void *parameter)
M
Ming, Bai 已提交
78
{
Y
yiyue.fang 已提交
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
    int result;
    void *stack;
    char name[RT_NAME_MAX];
    static rt_uint16_t pthread_number = 0;
    _pthread_data_t *ptd;

    /* tid shall be provided */
    RT_ASSERT(tid != RT_NULL);

    /* allocate posix thread data */
    ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
    if (ptd == RT_NULL)
        return ENOMEM;
    /* clean posix thread data memory */
    rt_memset(ptd, 0, sizeof(_pthread_data_t));
    ptd->canceled = 0;
    ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
    ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
    ptd->magic = PTHREAD_MAGIC;

    if (attr != RT_NULL)
        ptd->attr = *attr;
    else 
    {
        /* use default attribute */
        pthread_attr_init(&ptd->attr);
    }

    rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
    if (ptd->attr.stack_base == 0)
    {
        stack = (void*)rt_malloc(ptd->attr.stack_size);
    }
    else
        stack = (void*)(ptd->attr.stack_base);

    if (stack == RT_NULL) 
    {
        rt_free(ptd);

        return ENOMEM;
    }

    /* pthread is a static thread object */
    ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
    if (ptd->tid == RT_NULL)
    {
        if (ptd->attr.stack_base == 0)
            rt_free(stack);
        rt_free(ptd);

        return ENOMEM;
    }

    if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
    {
        ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
        if (ptd->joinable_sem == RT_NULL)
        {
            if (ptd->attr.stack_base != 0)
                rt_free(stack);
            rt_free(ptd);

            return ENOMEM;
        }
    }
    else
        ptd->joinable_sem = RT_NULL;

    /* set parameter */
    ptd->thread_entry = start;
    ptd->thread_parameter = parameter;

    /* initial this pthread to system */
    if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd, 
        stack, ptd->attr.stack_size, 
        ptd->attr.priority, 5) != RT_EOK)
    {
        if (ptd->attr.stack_base == 0)
            rt_free(stack);
        if (ptd->joinable_sem != RT_NULL)
            rt_sem_delete(ptd->joinable_sem);
        rt_free(ptd);

        return EINVAL;
    }

    /* set pthread id */
    *tid = ptd->tid;

    /* set pthread cleanup function and ptd data */
    (*tid)->cleanup = _pthread_cleanup;
    (*tid)->user_data = (rt_uint32_t)ptd;

    /* start thread */
    result = rt_thread_startup(*tid);
    if (result == RT_EOK)
        return 0;

    /* start thread failed */
    rt_thread_detach(ptd->tid);
    if (ptd->attr.stack_base == 0)
        rt_free(stack);
    if (ptd->joinable_sem != RT_NULL)
        rt_sem_delete(ptd->joinable_sem);

    rt_free(ptd);

    return EINVAL;
M
Ming, Bai 已提交
188 189 190 191 192
}
RTM_EXPORT(pthread_create);

int pthread_detach(pthread_t thread)
{
Y
yiyue.fang 已提交
193 194 195 196
    _pthread_data_t* ptd;

    ptd = _pthread_get_data(thread);

B
bernard 已提交
197
    if ((thread->stat & RT_THREAD_STAT_MASK)== RT_THREAD_CLOSE)
Y
yiyue.fang 已提交
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
    {
        /* delete joinable semaphore */
        if (ptd->joinable_sem != RT_NULL)
            rt_sem_delete(ptd->joinable_sem);
        /* detach thread object */
        rt_thread_detach(ptd->tid);

        /* release thread resource */
        if (ptd->attr.stack_base == RT_NULL)
        {
            /* release thread allocated stack */
            rt_free(ptd->tid->stack_addr);
        }

        /*
         * if this thread create the local thread data,
         * delete it
         */
        if (ptd->tls != RT_NULL)
            rt_free(ptd->tls);
        rt_free(ptd->tid);
        rt_free(ptd);
    }
    else
    {
        rt_enter_critical();
        /* change to detach state */
        ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;

        /* detach joinable semaphore */
        rt_sem_delete(ptd->joinable_sem);
        ptd->joinable_sem = RT_NULL;
        rt_exit_critical();
    }

    return 0;
M
Ming, Bai 已提交
234 235 236 237 238
}
RTM_EXPORT(pthread_detach);

int pthread_join (pthread_t thread, void **value_ptr)
{
Y
yiyue.fang 已提交
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
    _pthread_data_t* ptd;
    rt_err_t result;

    if (thread == rt_thread_self())
    {
        /* join self */
        return EDEADLK;
    }

    ptd = _pthread_get_data(thread);
    if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
        return EINVAL; /* join on a detached pthread */

    result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
    if (result == RT_EOK)
    {
        /* get return value */
        if (value_ptr != RT_NULL)
            *value_ptr = ptd->return_value;

        /* release resource */
        pthread_detach(thread);
    }
    else
        return ESRCH;
    
    return 0;
M
Ming, Bai 已提交
266 267 268
}
RTM_EXPORT(pthread_join);

Y
yiyue.fang 已提交
269
void pthread_exit (void *value)
M
Ming, Bai 已提交
270
{
Y
yiyue.fang 已提交
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
    _pthread_data_t *ptd;
    _pthread_cleanup_t *cleanup;
    extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];

    ptd = _pthread_get_data(rt_thread_self());

    rt_enter_critical();
    /* disable cancel */
    ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
    /* set return value */
    ptd->return_value = value;
    rt_exit_critical();

    /* invoke pushed cleanup */
    while (ptd->cleanup != RT_NULL)
    {
        cleanup = ptd->cleanup;
        ptd->cleanup = cleanup->next;

        cleanup->cleanup_func(cleanup->parameter);
        /* release this cleanup function */
        rt_free(cleanup);
    }

    /* destruct thread local key */
    if (ptd->tls != RT_NULL)
    {
        void *data;
        rt_uint32_t index;
        
        for (index = 0; index < PTHREAD_KEY_MAX; index ++)
        {
            if (_thread_keys[index].is_used)
            {
                data = ptd->tls[index];
                if (data)
                    _thread_keys[index].destructor(data);
            }
        }

        /* release tls area */
        rt_free(ptd->tls);
        ptd->tls = RT_NULL;
    }

    if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
    {
        /* release the joinable pthread */
        rt_sem_release(ptd->joinable_sem);
    }

    /* detach thread */
    rt_thread_detach(ptd->tid);
    /* reschedule thread */
    rt_schedule();
M
Ming, Bai 已提交
326 327 328
}
RTM_EXPORT(pthread_exit);

Y
yiyue.fang 已提交
329
int pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
M
Ming, Bai 已提交
330
{
Y
yiyue.fang 已提交
331 332
    RT_ASSERT(once_control != RT_NULL);
    RT_ASSERT(init_routine != RT_NULL);
M
Ming, Bai 已提交
333

Y
yiyue.fang 已提交
334 335 336 337 338 339
    rt_enter_critical();
    if (!(*once_control))
    {
        /* call routine once */
        *once_control = 1;
        rt_exit_critical();
M
Ming, Bai 已提交
340

Y
yiyue.fang 已提交
341 342 343
        init_routine();
    }
    rt_exit_critical();
M
Ming, Bai 已提交
344

Y
yiyue.fang 已提交
345
    return 0;
M
Ming, Bai 已提交
346 347 348 349 350
}
RTM_EXPORT(pthread_once);

int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
{
B
bernard 已提交
351
    return EOPNOTSUPP;
M
Ming, Bai 已提交
352 353 354 355 356
}
RTM_EXPORT(pthread_atfork);

int pthread_kill(pthread_t thread, int sig)
{
B
bernard 已提交
357 358 359 360 361
#ifdef RT_USING_SIGNALS
    return rt_thread_kill(thread, sig);
#else
	return ENOSYS;
#endif
M
Ming, Bai 已提交
362 363 364
}
RTM_EXPORT(pthread_kill);

B
bernard 已提交
365 366 367 368 369 370 371
#ifdef RT_USING_SIGNALS
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
{
	return sigprocmask(how, set, oset);
}
#endif

M
Ming, Bai 已提交
372 373
void pthread_cleanup_pop(int execute)
{
Y
yiyue.fang 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    _pthread_data_t *ptd;
    _pthread_cleanup_t *cleanup;

    /* get posix thread data */
    ptd = _pthread_get_data(rt_thread_self());
    RT_ASSERT(ptd != RT_NULL);

    if (execute)
    {
        rt_enter_critical();
        cleanup = ptd->cleanup;
        if (cleanup)
            ptd->cleanup = cleanup->next;
        rt_exit_critical();

        if (cleanup)
        {
            cleanup->cleanup_func(cleanup->parameter);

            rt_free(cleanup);
        }
    }
M
Ming, Bai 已提交
396 397 398 399 400
}
RTM_EXPORT(pthread_cleanup_pop);

void pthread_cleanup_push(void (*routine)(void*), void *arg)
{
Y
yiyue.fang 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    _pthread_data_t *ptd;
    _pthread_cleanup_t *cleanup;

    /* get posix thread data */
    ptd = _pthread_get_data(rt_thread_self());
    RT_ASSERT(ptd != RT_NULL);

    cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
    if (cleanup != RT_NULL)
    {
        cleanup->cleanup_func = routine;
        cleanup->parameter = arg;

        rt_enter_critical();
        cleanup->next = ptd->cleanup;
        ptd->cleanup = cleanup;
        rt_exit_critical();
    }
M
Ming, Bai 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
}
RTM_EXPORT(pthread_cleanup_push);

/*
 * According to IEEE Std 1003.1, 2004 Edition , following pthreads
 * interface support cancellation point:
 * mq_receive()
 * mq_send()
 * mq_timedreceive()
 * mq_timedsend()
 * msgrcv()
 * msgsnd()
 * msync()
 * pthread_cond_timedwait()
 * pthread_cond_wait()
 * pthread_join()
 * pthread_testcancel()
 * sem_timedwait()
 * sem_wait()
 *
 * A cancellation point may also occur when a thread is
 * executing the following functions:
 * pthread_rwlock_rdlock()
 * pthread_rwlock_timedrdlock()
 * pthread_rwlock_timedwrlock()
 * pthread_rwlock_wrlock()
 *
 * The pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()
 * functions are defined to be async-cancel safe.
 */

int pthread_setcancelstate(int state, int *oldstate)
{
Y
yiyue.fang 已提交
452
    _pthread_data_t *ptd;
M
Ming, Bai 已提交
453

Y
yiyue.fang 已提交
454 455 456
    /* get posix thread data */
    ptd = _pthread_get_data(rt_thread_self());
    RT_ASSERT(ptd != RT_NULL);
M
Ming, Bai 已提交
457

Y
yiyue.fang 已提交
458 459 460 461 462
    if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
    {
        if (oldstate)
            *oldstate = ptd->cancelstate;
        ptd->cancelstate = state;
M
Ming, Bai 已提交
463

Y
yiyue.fang 已提交
464 465
        return 0;
    }
M
Ming, Bai 已提交
466

Y
yiyue.fang 已提交
467
    return EINVAL;
M
Ming, Bai 已提交
468 469 470 471 472
}
RTM_EXPORT(pthread_setcancelstate);

int pthread_setcanceltype(int type, int *oldtype)
{
Y
yiyue.fang 已提交
473
    _pthread_data_t *ptd;
M
Ming, Bai 已提交
474

Y
yiyue.fang 已提交
475 476 477
    /* get posix thread data */
    ptd = _pthread_get_data(rt_thread_self());
    RT_ASSERT(ptd != RT_NULL);
M
Ming, Bai 已提交
478

Y
yiyue.fang 已提交
479 480
    if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS)) 
        return EINVAL;
M
Ming, Bai 已提交
481

Y
yiyue.fang 已提交
482 483 484
    if (oldtype)
        *oldtype = ptd->canceltype;
    ptd->canceltype = type;
M
Ming, Bai 已提交
485

Y
yiyue.fang 已提交
486
    return 0;
M
Ming, Bai 已提交
487 488 489 490 491
}
RTM_EXPORT(pthread_setcanceltype);

void pthread_testcancel(void)
{
Y
yiyue.fang 已提交
492 493
    int cancel=0;
    _pthread_data_t* ptd;
M
Ming, Bai 已提交
494

Y
yiyue.fang 已提交
495 496 497
    /* get posix thread data */
    ptd = _pthread_get_data(rt_thread_self());
    RT_ASSERT(ptd != RT_NULL);
M
Ming, Bai 已提交
498

Y
yiyue.fang 已提交
499 500 501 502
    if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
        cancel = ptd->canceled;
    if (cancel)
        pthread_exit((void*)PTHREAD_CANCELED);
M
Ming, Bai 已提交
503 504 505 506 507
}
RTM_EXPORT(pthread_testcancel);

int pthread_cancel(pthread_t thread)
{
Y
yiyue.fang 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    _pthread_data_t *ptd;

    /* cancel self */
    if (thread == rt_thread_self())
        return 0;

    /* get posix thread data */
    ptd = _pthread_get_data(thread);
    RT_ASSERT(ptd != RT_NULL);

    /* set canceled */
    if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
    {
        ptd->canceled = 1;
        if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
        {
            /*
             * to detach thread.
             * this thread will be removed from scheduler list
             * and because there is a cleanup function in the
             * thread (pthread_cleanup), it will move to defunct
             * thread list and wait for handling in idle thread.
             */
            rt_thread_detach(thread);
        }
    }

    return 0;
M
Ming, Bai 已提交
536 537 538
}
RTM_EXPORT(pthread_cancel);