Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2259 → Rev 2260

/branches/rcu/kernel/test/test.c
57,6 → 57,7
#include <print/print1.def>
#include <thread/thread1.def>
#include <sysinfo/sysinfo1.def>
#include <tasklet/tasklet1.def>
{NULL, NULL, NULL}
};
 
/branches/rcu/kernel/test/test.h
69,6 → 69,7
extern char * test_print1(bool quiet);
extern char * test_thread1(bool quiet);
extern char * test_sysinfo1(bool quiet);
extern char * test_tasklet1(bool quiet);
 
extern test_t tests[];
 
/branches/rcu/kernel/test/tasklet/tasklet1.def
0,0 → 1,6
{
"tasklet1",
"Tasklet test (very basic)",
&test_tasklet1,
true
},
/branches/rcu/kernel/test/tasklet/tasklet1.c
0,0 → 1,103
/*
* Copyright (c) 2007 Jan Hudecek
* Copyright (c) 2005 Jakub Jermar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
#include <print.h>
 
#include <test.h>
#include <ddi/tasklet.h>
#include <synch/waitq.h>
 
#include <arch/types.h>
 
static void func(void *data)
{
printf("%s", data);
}
 
char * test_tasklet1(bool quiet)
{
waitq_t wq;
waitq_initialize(&wq);
tasklet_descriptor_t *tasklet_desc;
 
//before we start we need to register a tasklet
if (!quiet)
printf("Registering tasklet...");
if (!quiet)
tasklet_desc=tasklet_register(&func, "\nTasklet called and received data\n");
else
tasklet_desc=tasklet_register(&func, "");
if (!quiet)
printf("Done!\n");
 
//first we'll try disabling the tasklet
if (!quiet)
printf("Disabling tasklet...");
tasklet_disable(tasklet_desc);
if (!quiet)
printf("Done!\n");
 
//we'll schedule the disabled tasklet
if (!quiet)
printf("Scheduling tasklet...");
tasklet_schedule(tasklet_desc);
if (!quiet)
printf("Done!\n");
 
//and we'll wait if it gets called. It shouldn't however because it's disabled
if (!quiet)
printf("Waiting 5s...");
waitq_sleep_timeout(&wq,(uint32_t) 5000000,0);
if (!quiet)
printf("Done!\n");
 
//then we'll try to enable it
if (!quiet)
printf("Enabling tasklet...");
tasklet_enable(tasklet_desc);
if (!quiet)
printf("Done!\n");
//and wait if it gets called this time. It should beacause it's enabled
if (!quiet)
printf("Waiting 5s...");
waitq_sleep_timeout(&wq,(uint32_t) 5000000,0);
if (!quiet)
printf("Done!\n");
 
//finally we'll free the tasklet structure
if (!quiet)
printf("Freeing...");
tasklet_free(tasklet_desc);
if (!quiet)
printf("Done!\n");
 
 
return NULL;
}
/branches/rcu/kernel/generic/include/synch/rcu.h
0,0 → 1,61
/*
* Copyright (c) 2007 Jan Hudecek
* Copyright (c) 2006 Jakub Jermar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup sync
* @{
*/
/** @file
*/
 
#ifndef KERN_RCU_H_
#define KERN_RCU_H_
 
#include <arch/types.h>
#include <ddi/tasklet.h>
#include <arch/barrier.h>
#include <preemption.h>
 
 
/** Read lock for RCU protected pointer */
#define rcu_read_lock preemption_disable()
 
/** Release of read lock for RCU protected pointer */
#define rcu_read_unlock preemption_enable()
 
/** Dereferencing of an RCU protected pointer */
#define rcu_dereference_pointer(p) (*(p))
 
/** Assigning a value to an RCU protected pointer */
#define rcu_assign_pointer(p, newp) {write_barrier(); (p)=(newp)}
 
void rcu_init(void);
void rcu_synchronize(void);
void rcu_synchronize_callback_function(void* waitq);
void rcu_sync_callback(void (*func)(void* data), void* data);
#endif
/branches/rcu/kernel/generic/include/ddi/tasklet.h
0,0 → 1,104
/*
* Copyright (c) 2007 Jan Hudecek
* Copyright (c) 2006 Jakub Jermar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup genericddi
* @{
*/
/** @file
*/
 
#ifndef KERN_TASKLET_H_
#define KERN_TASKLET_H_
 
#include <arch/types.h>
#include <proc/task.h>
 
 
 
/** Structure describing a tasklet*/
typedef struct tasklet_descriptor
{
/** callback to call */
void (*func)(void* data);
/** parameters to pass to func */
void* data;
/** state of the tasklet one of tasklet_state_enum */
uint32_t state;
struct tasklet_descriptor *next;
} tasklet_descriptor_t;
 
/** Initializes tasklets - except for the tasklet thread */
void tasklet_init(void);
 
/**
* Creates and runs the tasklet thread
*
* @param kernel_task Pointer to the kernel task - for create_thread
*/
void tasklet_run_tasklet_thread(task_t * kernel_task);
 
/** Thread which keeps executing scheduled enabled tasklets
* @param data not used
*/
void tasklet_thread(void* data);
 
 
/** Executes scheduled enabled tasklets */
void tasklet_do(void);
 
/** Initializes tasklet structure
* @param func tasklet callback function to be called
* @param data pointer to be passed to the tasklet function
*/
tasklet_descriptor_t* tasklet_register(void (*func)(void* data), void* data);
 
/** Schedules the tasklet for execution on current CPU
* @param t tasklet to be scheduled
*/
void tasklet_schedule(tasklet_descriptor_t* t);
 
/** Tasklet will not be run, even if scheduled
* @param t tasklet to be disabled
*/
void tasklet_disable(tasklet_descriptor_t* t);
 
/** Tasklet will be run if scheduled
* @param t tasklet to be enabled
*/
void tasklet_enable(tasklet_descriptor_t* t);
 
/** Frees the tasklet structure when no longer needed
* @param tasklet to be freed
*/
void tasklet_free(tasklet_descriptor_t* t);
 
#endif
 
/** @}
*/
/branches/rcu/kernel/generic/src/synch/rcu.c
0,0 → 1,122
/*
* Copyright (c) 2007 Jan Hudecek
* Copyright (c) 2006 Jakub Jermar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup sync
* @{
*/
/** @file
*/
 
#include <synch/rcu.h>
#include <synch/waitq.h>
#include <arch.h>
#include <config.h>
#include <arch/types.h>
#include <ddi/tasklet.h>
#include <synch/spinlock.h>
#include <time/delay.h>
#include <panic.h>
#include <print.h>
 
typedef struct rcu_global
{
uint32_t current_batch;
uint32_t completed_batch;
bool next_batch_waiting;
} rcu_global_t;
 
typedef struct rcu_callback_list
{
struct rcu_callback_list* next;
void (*func)(void*);
void* data;
bool* cpu_mask;
} rcu_callback_list_t;
 
 
typedef struct rcu_percpu
{
uint32_t current_batch_number;
uint32_t QS_passed;
bool QS_pending;
 
rcu_callback_list_t* next_batch, *current_batch, *done_batch;
} rcu_percpu_t;
 
rcu_global_t _rcu_global;
rcu_percpu_t* _rcu_cpu_lists;
 
void rcu_init(void)
{
_rcu_cpu_lists = malloc(sizeof(rcu_percpu_t)*config.cpu_count,0);
_rcu_global.completed_batch = -1;
_rcu_global.current_batch = -1;
_rcu_global.next_batch_waiting = -1;
 
}
 
void rcu_synchronize(void)
{
waitq_t wq;
waitq_initialize(&wq);
rcu_sync_callback(rcu_synchronize_callback_function, &wq);
waitq_sleep(&wq);
}
 
void rcu_synchronize_callback_function(void* waitq)
{
waitq_wakeup(((waitq_t*)waitq), true);
}
 
void rcu_sync_callback(void (*func)(void* data), void* data)
{
int i;
rcu_callback_list_t *rd;
rd = malloc(sizeof(rcu_desc), 0);
rd->func = func;
rd->data = data;
rd->next = NULL;
 
rd->cpu_mask = malloc (sizeof(bool)*config.cpu_count,0);
for (i=0;i<config.cpu_count;i++)
rd->cpu_mask[i]=false;
i = ++(_rcu_global.current_batch);
_rcu_global.next_batch_waiting = true;
 
rd->next = _rcu_cpu_lists[0].next_batch;
for (i=0;i<config.cpu_count;i++)
{
_rcu_cpu_lists[i].next_batch = rd;
_rcu_cpu_lists[i].QS_pending = true;
 
}
 
//TODO:tasklet, after_thread_ran, polishing
}
 
/branches/rcu/kernel/generic/src/main/main.c
81,6 → 81,7
#include <console/klog.h>
#include <smp/smp.h>
#include <ddi/ddi.h>
#include <ddi/tasklet.h>
 
/** Global configuration structure. */
config_t config;
218,6 → 219,8
page_init();
tlb_init();
ddi_init();
tasklet_init();
// tasklet_do();
arch_post_mm_init();
 
version_print();
236,14 → 239,17
printf("config.memory_size=%zdM\n", config.memory_size >> 20);
printf("config.cpu_count=%zd\n", config.cpu_count);
cpu_init();
 
calibrate_delay_loop();
clock_counter_init();
// tasklet_do();
timeout_init();
scheduler_init();
task_init();
thread_init();
futex_init();
 
 
klog_init();
if (init.cnt > 0) {
255,6 → 261,7
printf("No init binaries found\n");
ipc_init();
//tasklet_do();
 
/*
* Create kernel task.
270,6 → 277,8
if (!t)
panic("can't create kinit thread\n");
thread_ready(t);
 
tasklet_run_tasklet_thread(k);
/*
* This call to scheduler() will return to kinit,
/branches/rcu/kernel/generic/src/ddi/tasklet.c
0,0 → 1,224
/*
* Copyright (c) 2007 Jan Hudecek
* Copyright (c) 2006 Jakub Jermar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup genericddi
* @{
*/
/** @file
*/
 
#include <arch.h>
#include <config.h>
#include <arch/types.h>
#include <arch/asm.h>
#include <ddi/tasklet.h>
#include <synch/spinlock.h>
#include <time/delay.h>
#include <proc/thread.h>
#include <panic.h>
#include <print.h>
#include <synch/waitq.h>
 
/** Timeout length in the tasklet thread */
#define TASKLET_THREAD_SLEEPDELAY 500
/** Priority of the tasklet thread */
#define TASKLET_THREAD_PRIORITY 10
 
 
/** Tasklet state enum */
typedef enum
{
TASKLET_STATE_NOTACTIVE = 0,
TASKLET_STATE_SCHEDULED = 1,
TASKLET_STATE_RUNNING = 2,
TASKLET_STATE_DISABLED = 4
} tasklet_state_enum;
 
 
 
/** Spinlock protecting list of tasklets */
SPINLOCK_INITIALIZE(tasklet_lock);
 
/** array of tasklet lists for every CPU */
tasklet_descriptor_t** tasklet_list;
 
/** Initializes tasklets - except for the tasklet thread */
void tasklet_init(void)
{
int i;
tasklet_list=malloc(sizeof(tasklet_descriptor_t*)*config.cpu_count,0);
if (tasklet_list==0)
panic_printf("tasklet list mem not allocated\n");
for(i=0;i<config.cpu_count;i++)
{
tasklet_list[i]=0;
}
spinlock_initialize(&tasklet_lock, "tasklet_lock");
}
 
/**
* Creates and runs the tasklet thread
*
* @param kernel_task Pointer to the kernel task - for create_thread
*/
void tasklet_run_tasklet_thread(task_t * kernel_task)
{
thread_t* t= thread_create(&tasklet_thread, NULL, kernel_task, 0, "tasklet_thread", false);
if (t==NULL)
{
//wtf?
panic_printf("tasklet thread not created\n");
}
else
{
//dynamical priority should pick the best
// t->priority = TASKLET_THREAD_PRIORITY;
thread_ready(t);
}
}
 
/** Thread which keeps executing scheduled enabled tasklets
* @param data not used
*/
void tasklet_thread(void* data)
{
waitq_t wq;
waitq_initialize(&wq);
while (true)
{
tasklet_do();
waitq_sleep_timeout(&wq,(uint32_t)TASKLET_THREAD_SLEEPDELAY,0);
}
}
 
/** Initializes tasklet structure
* @param func tasklet callback function to be called
* @param data pointer to be passed to the tasklet function
*/
tasklet_descriptor_t* tasklet_register(void (*func)(void* data), void* data)
{
tasklet_descriptor_t* tasklet=malloc(sizeof(tasklet_descriptor_t),0);
tasklet->data = data;
tasklet->func = func;
tasklet->state = TASKLET_STATE_NOTACTIVE;
tasklet->next = 0;
return tasklet;
}
 
/** Schedules the tasklet for execution on current CPU
* @param t tasklet to be scheduled
*/
void tasklet_schedule(tasklet_descriptor_t* t)
{
spinlock_lock(&tasklet_lock);
//clear notactive, running and scheduled flags
t->state &= TASKLET_STATE_DISABLED;
//set the scheduled flag
t->state |= TASKLET_STATE_SCHEDULED;
t->next=tasklet_list[CPU->id];
tasklet_list[CPU->id]=t;
spinlock_unlock(&tasklet_lock);
}
 
/** Tasklet will not be run, even if scheduled
* @param t tasklet to be disabled
*/
void tasklet_disable(tasklet_descriptor_t* t)
{
spinlock_lock(&tasklet_lock);
//set the disabled flag
t->state |= TASKLET_STATE_DISABLED;
spinlock_unlock(&tasklet_lock);
}
 
/** Tasklet will be run if scheduled
* @param t tasklet to be enabled
*/
void tasklet_enable(tasklet_descriptor_t* t)
{
spinlock_lock(&tasklet_lock);
//clear the disabled flag
t->state &= ~TASKLET_STATE_DISABLED;
spinlock_unlock(&tasklet_lock);
}
 
 
 
 
/** Executes scheduled enabled tasklets */
void tasklet_do(void)
{
int i;
spinlock_lock(&tasklet_lock);
for(i=0;i<config.cpu_count;i++)
{
tasklet_descriptor_t* t = tasklet_list[CPU->id];
//empty the queue of tasklets
tasklet_list[CPU->id]=0;
while (t)
{
if (!(t->state & TASKLET_STATE_DISABLED))
{
if (t->func)
{
#ifdef DEBUG
printf("tasklet - calling tasklet!!!\n");
delay((uint32_t)1000000);
#endif
t->state = TASKLET_STATE_RUNNING;
t->func(t->data);
t->state = TASKLET_STATE_NOTACTIVE;
}
else
panic_printf("tasklet func NULL\n");
}
else
{ //return it back to the queue of scheduled tasklets
t->next = tasklet_list[CPU->id];
tasklet_list[CPU->id] = t;
}
t=t->next;
}
//TODO: switch to another CPU, synched with kcpulb,
}
spinlock_unlock(&tasklet_lock);
}
 
/** Frees the tasklet structure when no longer needed
* @param tasklet to be freed
*/
void tasklet_free(tasklet_descriptor_t* t)
{
if (t->state == TASKLET_STATE_NOTACTIVE)
free(t);
else
panic_printf("attempting to free an active tasklet");
}
 
 
/branches/rcu/kernel/Makefile
155,6 → 155,7
generic/src/ddi/ddi.c \
generic/src/ddi/irq.c \
generic/src/ddi/device.c \
generic/src/ddi/tasklet.c \
generic/src/interrupt/interrupt.c \
generic/src/main/main.c \
generic/src/main/kinit.c \
196,6 → 197,7
generic/src/synch/condvar.c \
generic/src/synch/rwlock.c \
generic/src/synch/mutex.c \
generic/src/synch/rcu.c \
generic/src/synch/semaphore.c \
generic/src/synch/waitq.c \
generic/src/synch/futex.c \
238,7 → 240,8
test/synch/semaphore2.c \
test/print/print1.c \
test/thread/thread1.c \
test/sysinfo/sysinfo1.c
test/sysinfo/sysinfo1.c \
test/tasklet/tasklet1.c
endif
 
## Experimental features