Subversion Repositories HelenOS-historic

Rev

Rev 1427 | Rev 1614 | 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. #include <futex.h>
  37. #include <assert.h>
  38. #include <async.h>
  39.  
  40. #ifndef PSTHREAD_INITIAL_STACK_PAGES_NO
  41. #define PSTHREAD_INITIAL_STACK_PAGES_NO 1
  42. #endif
  43.  
  44. static LIST_INITIALIZE(ready_list);
  45. static LIST_INITIALIZE(serialized_list);
  46. static LIST_INITIALIZE(manager_list);
  47.  
  48. static void psthread_exit(void) __attribute__ ((noinline));
  49. static void psthread_main(void);
  50.  
  51. static atomic_t psthread_futex = FUTEX_INITIALIZER;
  52. /** Count of real threads that are in async_serialized mode */
  53. static int serialized_threads; /* Protected by async_futex */
  54. /** Thread-local count of serialization. If >0, we must not preempt */
  55. static __thread serialization_count;
  56. /** Counter of threads residing in async_manager */
  57. static int threads_in_manager;
  58.  
  59. /** Setup PSthread information into TCB structure */
  60. psthread_data_t * psthread_setup()
  61. {
  62.     psthread_data_t *pt;
  63.     tcb_t *tcb;
  64.  
  65.     tcb = __make_tls();
  66.     if (!tcb)
  67.         return NULL;
  68.  
  69.     pt = malloc(sizeof(*pt));
  70.     if (!pt) {
  71.         __free_tls(tcb);
  72.         return NULL;
  73.     }
  74.  
  75.     tcb->pst_data = pt;
  76.     pt->tcb = tcb;
  77.  
  78.     return pt;
  79. }
  80.  
  81. void psthread_teardown(psthread_data_t *pt)
  82. {
  83.     __free_tls(pt->tcb);
  84.     free(pt);
  85. }
  86.  
  87. /** Function that is called on entry to new uspace thread */
  88. void psthread_main(void)
  89. {
  90.     psthread_data_t *pt = __tcb_get()->pst_data;
  91.  
  92.     serialization_count = 0; // TODO: WHY HERE?
  93.     pt->retval = pt->func(pt->arg);
  94.  
  95.     pt->finished = 1;
  96.     if (pt->waiter)
  97.         list_append(&pt->waiter->link, &ready_list);
  98.  
  99.     psthread_schedule_next_adv(PS_FROM_DEAD);
  100. }
  101.  
  102. /** Schedule next userspace pseudo thread.
  103.  *
  104.  * If calling with PS_TO_MANAGER parameter, the async_futex should be
  105.  * held.
  106.  *
  107.  * @param tomanager If true, we are switching to next ready manager thread
  108.  *                  (if none is found, thread is exited)
  109.  * @param frommanager If true, we are switching from manager thread
  110.  * @return 0 if there is no ready pseudo thread, 1 otherwise.
  111.  */
  112. int psthread_schedule_next_adv(pschange_type ctype)
  113. {
  114.     psthread_data_t *srcpt, *dstpt;
  115.     int retval = 0;
  116.    
  117.     futex_down(&psthread_futex);
  118.  
  119.     if (ctype == PS_PREEMPT && list_empty(&ready_list))
  120.         goto ret_0;
  121.  
  122.     if (ctype == PS_FROM_MANAGER) {
  123.         if (list_empty(&ready_list) && list_empty(&serialized_list))
  124.             goto ret_0;
  125.         /* Do not preempt if there is not sufficient count of thread managers */
  126.         if (list_empty(&serialized_list) && threads_in_manager <= serialized_threads) {
  127.             goto ret_0;
  128.         }
  129.     }
  130.     /* If we are going to manager and none exists, create it */
  131.     if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
  132.         while (list_empty(&manager_list)) {
  133.             futex_up(&psthread_futex);
  134.             async_create_manager();
  135.             futex_down(&psthread_futex);
  136.         }
  137.     }
  138.    
  139.     if (ctype != PS_FROM_DEAD) {
  140.         /* Save current state */
  141.         srcpt = __tcb_get()->pst_data;
  142.         if (!context_save(&srcpt->ctx)) {
  143.             if (serialization_count)
  144.                 srcpt->flags &= ~PSTHREAD_SERIALIZED;
  145.             return 1; // futex_up already done here
  146.         }
  147.  
  148.         /* Save myself to correct run list */
  149.         if (ctype == PS_PREEMPT)
  150.             list_append(&srcpt->link, &ready_list);
  151.         else if (ctype == PS_FROM_MANAGER) {
  152.             list_append(&srcpt->link, &manager_list);
  153.             threads_in_manager--;
  154.         } /* If ctype == PS_TO_MANAGER, don't save ourselves to any list, we should
  155.            * already be somewhere, or we will be lost */
  156.     }
  157.  
  158.     /* Choose new thread to run */
  159.     if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
  160.         dstpt = list_get_instance(manager_list.next,psthread_data_t, link);
  161.         if (serialization_count && ctype == PS_TO_MANAGER) {
  162.             serialized_threads++;
  163.             srcpt->flags |= PSTHREAD_SERIALIZED;
  164.         }
  165.         threads_in_manager++;
  166.     } else {
  167.         if (!list_empty(&serialized_list)) {
  168.             dstpt = list_get_instance(serialized_list.next, psthread_data_t, link);
  169.             serialized_threads--;
  170.         } else
  171.             dstpt = list_get_instance(ready_list.next, psthread_data_t, link);
  172.     }
  173.     list_remove(&dstpt->link);
  174.  
  175.     futex_up(&psthread_futex);
  176.     context_restore(&dstpt->ctx);
  177.  
  178. ret_0:
  179.     futex_up(&psthread_futex);
  180.     return retval;
  181. }
  182.  
  183. /** Wait for uspace pseudo thread to finish.
  184.  *
  185.  * @param psthrid Pseudo thread to wait for.
  186.  *
  187.  * @return Value returned by the finished thread.
  188.  */
  189. int psthread_join(pstid_t psthrid)
  190. {
  191.     volatile psthread_data_t *pt, *mypt;
  192.     volatile int retval;
  193.  
  194.     /* Handle psthrid = Kernel address -> it is wait for call */
  195.     pt = (psthread_data_t *) psthrid;
  196.  
  197.     /* TODO */
  198.     printf("join unsupported\n");
  199.     _exit(1);
  200.  
  201.     retval = pt->retval;
  202.  
  203.     free(pt->stack);
  204.     psthread_teardown((void *)pt);
  205.  
  206.     return retval;
  207. }
  208.  
  209. /**
  210.  * Create a userspace thread
  211.  *
  212.  * @param func Pseudo thread function.
  213.  * @param arg Argument to pass to func.
  214.  *
  215.  * @return 0 on failure, TLS of the new pseudo thread.
  216.  */
  217. pstid_t psthread_create(int (*func)(void *), void *arg)
  218. {
  219.     psthread_data_t *pt;
  220.  
  221.     pt = psthread_setup();
  222.     if (!pt)
  223.         return 0;
  224.     pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize());
  225.  
  226.     if (!pt->stack) {
  227.         psthread_teardown(pt);
  228.         return 0;
  229.     }
  230.  
  231.     pt->arg= arg;
  232.     pt->func = func;
  233.     pt->finished = 0;
  234.     pt->waiter = NULL;
  235.     pt->flags = 0;
  236.  
  237.     context_save(&pt->ctx);
  238.     context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(),
  239.             pt->tcb);
  240.  
  241.     return (pstid_t )pt;
  242. }
  243.  
  244. /** Add a thread to ready list */
  245. void psthread_add_ready(pstid_t psthrid)
  246. {
  247.     psthread_data_t *pt;
  248.  
  249.     pt = (psthread_data_t *) psthrid;
  250.     futex_down(&psthread_futex);
  251.     if ((pt->flags & PSTHREAD_SERIALIZED))
  252.         list_append(&pt->link, &serialized_list);
  253.     else
  254.         list_append(&pt->link, &ready_list);
  255.     futex_up(&psthread_futex);
  256. }
  257.  
  258. /** Add a thread to manager list */
  259. void psthread_add_manager(pstid_t psthrid)
  260. {
  261.     psthread_data_t *pt;
  262.  
  263.     pt = (psthread_data_t *) psthrid;
  264.  
  265.     futex_down(&psthread_futex);
  266.     list_append(&pt->link, &manager_list);
  267.     futex_up(&psthread_futex);
  268. }
  269.  
  270. /** Remove one manager from manager list */
  271. void psthread_remove_manager()
  272. {
  273.     futex_down(&psthread_futex);
  274.     if (list_empty(&manager_list)) {
  275.         futex_up(&psthread_futex);
  276.         return;
  277.     }
  278.     list_remove(manager_list.next);
  279.     futex_up(&psthread_futex);
  280. }
  281.  
  282. /** Return thread id of current running thread */
  283. pstid_t psthread_get_id(void)
  284. {
  285.     return (pstid_t)__tcb_get()->pst_data;
  286. }
  287.  
  288. /** Disable preemption
  289.  *
  290.  * If the thread wants to send several message in row and does not want
  291.  * to be preempted, it should start async_serialize_start() in the beginning
  292.  * of communication and async_serialize_end() in the end. If it is a
  293.  * true multithreaded application, it should protect the communication channel
  294.  * by a futex as well. Interrupt messages will can still be preempted.
  295.  */
  296. void psthread_inc_sercount(void)
  297. {
  298.     serialization_count++;
  299. }
  300.  
  301. void psthread_dec_sercount(void)
  302. {
  303.     serialization_count--;
  304. }
  305.