Subversion Repositories HelenOS-historic

Rev

Rev 1113 | Rev 1128 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2006 Ondrej Palkovsky
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. #include <libadt/list.h>
  30. #include <psthread.h>
  31. #include <malloc.h>
  32. #include <unistd.h>
  33. #include <thread.h>
  34. #include <stdio.h>
  35. #include <kernel/arch/faddr.h>
  36.  
  37. static LIST_INITIALIZE(ready_list);
  38.  
  39. static void ps_exit(void) __attribute__ ((noinline));
  40.  
  41. /** Function to preempt to other thread without adding
  42.  * currently running thread to runqueue
  43.  */
  44. void ps_exit(void)
  45. {
  46.     psthread_data_t *pt;
  47.  
  48.     if (list_empty(&ready_list)) {
  49.         /* Wait on IPC queue etc... */
  50.         printf("Cannot exit!!!\n");
  51.         _exit(0);
  52.     }
  53.     pt = list_get_instance(ready_list.next, psthread_data_t, list);
  54.     list_remove(&pt->list);
  55.     context_restore(&pt->ctx);
  56. }
  57.  
  58. /** Function that is called on entry to new uspace thread */
  59. static int psthread_main(void)
  60. {
  61.     psthread_data_t *pt = __tls_get();
  62.     pt->retval = pt->func(pt->arg);
  63.  
  64.     pt->finished = 1;
  65.     if (pt->waiter)
  66.         list_append(&pt->waiter->list, &ready_list);
  67.  
  68.     ps_exit();
  69. }
  70.  
  71. /** Do a preemption of userpace threads */
  72. int ps_preempt(void)
  73. {
  74.     psthread_data_t *pt;
  75.  
  76.     if (list_empty(&ready_list))
  77.         return 0;
  78.  
  79.     pt = __tls_get();
  80.     if (! context_save(&pt->ctx))
  81.         return 1;
  82.    
  83.     list_append(&pt->list, &ready_list);
  84.     pt = list_get_instance(ready_list.next, psthread_data_t, list);
  85.     list_remove(&pt->list);
  86.  
  87.     context_restore(&pt->ctx);
  88. }
  89.  
  90. /** Wait for uspace thread to finish */
  91. int ps_join(pstid_t psthrid)
  92. {
  93.     volatile psthread_data_t *pt, *mypt;
  94.     volatile int retval;
  95.  
  96.     /* Handle psthrid = Kernel address -> it is wait for call */
  97.  
  98.     pt = (psthread_data_t *) psthrid;
  99.  
  100.     if (!pt->finished) {
  101.         mypt = __tls_get();
  102.         if (context_save(&((psthread_data_t *) mypt)->ctx)) {
  103.             pt->waiter = (psthread_data_t *) mypt;
  104.             ps_exit();
  105.         }
  106.     }
  107.     retval = pt->retval;
  108.  
  109.     free(pt->stack);
  110.     __free_tls((psthread_data_t *) pt);
  111.  
  112.     return retval;
  113. }
  114.  
  115. /**
  116.  * Create a userspace thread
  117.  *
  118.  */
  119. pstid_t psthread_create(int (*func)(void *), void *arg)
  120. {
  121.     psthread_data_t *pt;
  122.  
  123.     pt = __make_tls();
  124.     pt->stack = (char *) malloc(getpagesize());
  125.  
  126.     if (!pt->stack) {
  127.         return 0;
  128.     }
  129.  
  130.     pt->arg= arg;
  131.     pt->func = func;
  132.     pt->finished = 0;
  133.     pt->waiter = NULL;
  134.  
  135.     context_save(&pt->ctx);
  136.     context_set(&pt->ctx, FADDR(psthread_main), pt->stack, getpagesize(), pt);
  137.  
  138.     list_append(&pt->list, &ready_list);
  139.  
  140.     return (pstid_t )pt;
  141. }
  142.  
  143.