Subversion Repositories HelenOS

Rev

Rev 1125 | 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.  
  36. static LIST_INITIALIZE(ready_list);
  37.  
  38. /** Function to preempt to other thread without adding
  39.  * currently running thread to runqueue
  40.  */
  41. static void ps_exit(void)
  42. {
  43.     psthread_data_t *pt;
  44.  
  45.     if (list_empty(&ready_list)) {
  46.         /* Wait on IPC queue etc... */
  47.         printf("Cannot exit!!!\n");
  48.         _exit(0);
  49.     }
  50.     pt = list_get_instance(ready_list.next, psthread_data_t, list);
  51.     list_remove(&pt->list);
  52.     context_restore(&pt->ctx);
  53. }
  54.  
  55. /** Function that is called on entry to new uspace thread */
  56. static int psthread_main(void)
  57. {
  58.     psthread_data_t *pt = __tls_get();
  59.     pt->retval = pt->func(pt->arg);
  60.  
  61.     pt->finished = 1;
  62.     if (pt->waiter)
  63.         list_append(&pt->waiter->list, &ready_list);
  64.  
  65.     ps_exit();
  66. }
  67.  
  68. /** Do a preemption of userpace threads */
  69. int ps_preempt(void)
  70. {
  71.     psthread_data_t *pt;
  72.  
  73.     if (list_empty(&ready_list))
  74.         return 0;
  75.  
  76.     pt = __tls_get();
  77.     if (! context_save(&pt->ctx))
  78.         return 1;
  79.    
  80.     list_append(&pt->list, &ready_list);
  81.     pt = list_get_instance(ready_list.next, psthread_data_t, list);
  82.     list_remove(&pt->list);
  83.  
  84.     context_restore(&pt->ctx);
  85. }
  86.  
  87. /** Wait for uspace thread to finish */
  88. int ps_join(pstid_t psthrid)
  89. {
  90.     psthread_data_t *pt, *mypt;
  91.     int retval;
  92.  
  93.     /* Handle psthrid = Kernel address -> it is wait for call */
  94.  
  95.     pt = (psthread_data_t *) psthrid;
  96.  
  97.     if (!pt->finished) {
  98.         mypt = __tls_get();
  99.         if (context_save(&mypt->ctx)) {
  100.             pt->waiter = mypt;
  101.             ps_exit();
  102.         }
  103.     }
  104.     retval = pt->retval;
  105.  
  106.     free(pt->stack);
  107.     __free_tls(pt);
  108.  
  109.     return retval;
  110. }
  111.  
  112. /**
  113.  * Create a userspace thread
  114.  *
  115.  */
  116. pstid_t psthread_create(int (*func)(void *), void *arg)
  117. {
  118.     psthread_data_t *pt;
  119.  
  120.     pt = __make_tls();
  121.     pt->stack = (char *) malloc(getpagesize());
  122.  
  123.     if (!pt->stack) {
  124.         return 0;
  125.     }
  126.  
  127.     pt->arg= arg;
  128.     pt->func = func;
  129.     pt->finished = 0;
  130.     pt->waiter = NULL;
  131.  
  132.     context_save(&pt->ctx);
  133.     context_set(&pt->ctx, psthread_main, pt->stack, getpagesize(), pt);
  134.  
  135.     list_append(&pt->list, &ready_list);
  136.  
  137.     return (pstid_t )pt;
  138. }
  139.  
  140.