/*
* 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 <proc/tasklet.h>
#include <synch/spinlock.h>
#include <time/delay.h>
#include <panic.h>
#include <print.h>
SPINLOCK_INITIALIZE(rcu_global_lock);
typedef struct rcu_callback_list {
struct rcu_callback_list* next;
void (*func)(void*);
void* data;
} rcu_callback_list_t;
typedef struct {
#ifdef CONFIG_SMP
bool* cpu_mask;
#endif
rcu_callback_list_t* next_batch, *current_batch, *done_batch;
} rcu_global_t;
rcu_global_t* _rcu_global;
tasklet_descriptor_t* rcu_tasklet_desc;
void rcu_init(void)
{
#ifdef CONFIG_SMP
int i;
#endif
_rcu_global
= malloc(sizeof(rcu_global_t
),0);
_rcu_global->done_batch = NULL;
_rcu_global->current_batch = NULL;
_rcu_global->next_batch = NULL;
spinlock_initialize(&rcu_global_lock, "rcu_global_lock");
rcu_tasklet_desc = tasklet_register(&rcu_tasklet, NULL);
tasklet_disable(rcu_tasklet_desc);
#ifdef CONFIG_SMP
_rcu_global
->cpu_mask
= malloc (sizeof(bool
)*config.
cpu_count,0);
for (i=0;i<config.cpu_count;i++) {
_rcu_global->cpu_mask[i]=false;
}
#else
tasklet_schedule(rcu_tasklet_desc);
#endif
tasklet_enable(rcu_tasklet_desc);
}
void rcu_synchronize(void)
{
#ifdef CONFIG_SMP
waitq_t
*wq
= malloc(sizeof(waitq_t
),0);
waitq_initialize(wq);
rcu_sync_callback(&rcu_synchronize_callback_function, wq);
waitq_sleep(wq);
#endif
}
#ifdef CONFIG_SMP
void rcu_synchronize_callback_function(void* waitq)
{
waitq_wakeup(((waitq_t*)waitq), WAKEUP_ALL_INC_MISSED);
}
#endif
void rcu_sync_callback(void (*func)(void* data), void* data)
{
#ifndef CONFIG_SMP
func(data);
#else
int i;
rcu_callback_list_t *rd;
rd
= malloc(sizeof(rcu_callback_list_t
), 0);
rd->func = func;
rd->data = data;
rd->next = NULL;
printf("synccallback locking \n");
spinlock_lock(&rcu_global_lock);
rd->next = _rcu_global->next_batch;
_rcu_global->next_batch = rd;
if (_rcu_global->current_batch == NULL) {
_rcu_global->current_batch = _rcu_global->next_batch;
_rcu_global->next_batch = NULL;
printf("setting callback %x as current\n",&rd
->func
);
for (i=0;i<config.cpu_count;i++)
_rcu_global->cpu_mask[i]=false;
//we've surely passed the quiescent point just by running this method
rcu_passQS();
}
for (i=0;i<config.cpu_count;i++) {
tasklet_schedule_SMP(rcu_tasklet_desc, i);
}
spinlock_unlock(&rcu_global_lock);
printf ("sync callback called,unlocking, state:%x \n",rcu_tasklet_desc
->state
);
#endif
}
//TODO: polishing, comments
void rcu_tasklet(void* data)
{
rcu_callback_list_t* rd;
bool passed_all_QS;
#ifdef CONFIG_SMP
int i;
#endif
rcu_passQS();
passed_all_QS = true;
spinlock_lock(&rcu_global_lock);
#ifdef CONFIG_SMP
for (i = 0; i < config.cpu_active; i++)
passed_all_QS &= _rcu_global->cpu_mask[i];
#endif
if (passed_all_QS) {
if (_rcu_global->done_batch) {
rd = _rcu_global->done_batch;
while (rd->next) rd = rd->next;
//append the current list to done list
rd->next = _rcu_global->current_batch;
} else
_rcu_global->done_batch = _rcu_global->current_batch;
printf("setting callback %x as done\n",&_rcu_global
->current_batch
->func
);
_rcu_global->current_batch = NULL;
}
_rcu_global->current_batch = _rcu_global->next_batch;
_rcu_global->next_batch = NULL;
if (_rcu_global->current_batch == NULL) {
//there are no rcu callbacks registered, there is no need to monitor QS
printf("tasklet idle disabling \n");
// tasklet_disable(rcu_tasklet_desc);
spinlock_unlock(&rcu_global_lock);
} else
spinlock_unlock(&rcu_global_lock);
printf("tasklet unlocking \n");
}
inline void rcu_passQS(void)
{
#ifdef CONFIG_SMP
_rcu_global->cpu_mask[CPU->id] = true;
#endif
}
void rcu_run_callbacks(void)
{
rcu_callback_list_t* rd;
rcu_passQS();
if (_rcu_global->done_batch) {
spinlock_lock(&rcu_global_lock);
for (rd = _rcu_global->done_batch; rd; rd=rd->next) {
printf("calling %x \n",&rd
->func
);
rd->func(&rd->data);
}
_rcu_global->done_batch = NULL;
spinlock_unlock(&rcu_global_lock);
}
}