Skip to content

Instantly share code, notes, and snippets.

@matthewmorrone
Last active December 27, 2015 15:29
Show Gist options
  • Save matthewmorrone/71a3c12f447ca1aee11d to your computer and use it in GitHub Desktop.
Save matthewmorrone/71a3c12f447ca1aee11d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include "../../RW_sem.h"
typedef struct q_node
{
struct task_struct* task;
struct q_node* next;
} q_node;
typedef struct ProcQ
{
q_node* head;
q_node* tail;
} ProcQ;
int count = 0;
static void enqueue(struct RW_Sem* sem, struct task_struct* task)
{
q_node* temp;
temp = (q_node*)kmalloc(sizeof(q_node), GFP_KERNEL);
temp->task = task;
temp->next = NULL;
if (sem->queue->head == NULL)
{
sem->queue->head = temp;
sem->queue->tail = sem->queue->head;
}
else
{
temp->next = sem->queue->tail;
sem->queue->tail = temp;
}
count++;
temp = tail;
}
static struct task_struct* dequeue(struct RW_Sem* sem)
{
if (sem->queue->head == NULL) {return NULL;}
q_node* temp;
temp = sem->queue->head;
if(temp->next == NULL)
{
kfree(sem->queue->head);
sem->queue->head = NULL;
}
else
{
while(temp->next != sem->queue->head)
{
temp = temp->next;
}
kfree(sem->queue->head);
sem->queue->head = temp;
}
count--;
task_struct* temp_task = temp->task;
kfree(temp);
return temp_task;
}
DEFINE_SPINLOCK(sem_lock);
task_struct sleeping_task;
asmlinkage long sys_RW_wait(struct RW_Sem *sem)
{
spin_lock(&sem_lock);
if (sem->value > 0)
{
sem->value--;
sleeping_task = current;
set_current_state(TASK_INTERRUPTIBLE);
schedule();
}
spin_unlock(&sem_lock);
return 0;
}
asmlinkage long sys_RW_signal(struct RW_Sem *sem)
{
spin_lock(&sem_lock);
sem->value++;
if (sem->queue->head == NULL)
{
wake_up_process(sleeping_task);
}
spin_unlock(&sem_lock);
return 0;
}
// RW_queue.c:25: warning: ‘struct RW_Sem’ declared inside parameter list
// RW_queue.c:25: warning: its scope is only this definition or declaration, which is probably not what you want
// RW_queue.c: In function ‘enqueue’:
// RW_queue.c:28: error: ‘GFP_KERNEL’ undeclared (first use in this function)
// RW_queue.c:28: error: (Each undeclared identifier is reported only once
// RW_queue.c:28: error: for each function it appears in.)
// RW_queue.c:32: error: dereferencing pointer to incomplete type
// RW_queue.c:34: error: dereferencing pointer to incomplete type
// RW_queue.c:35: error: dereferencing pointer to incomplete type
// RW_queue.c:35: error: dereferencing pointer to incomplete type
// RW_queue.c:39: error: dereferencing pointer to incomplete type
// RW_queue.c:40: error: dereferencing pointer to incomplete type
// RW_queue.c:44: error: ‘tail’ undeclared (first use in this function)
// RW_queue.c: At top level:
// RW_queue.c:48: warning: ‘struct RW_Sem’ declared inside parameter list
// RW_queue.c: In function ‘dequeue’:
// RW_queue.c:50: error: dereferencing pointer to incomplete type
// RW_queue.c:53: error: dereferencing pointer to incomplete type
// RW_queue.c:57: error: dereferencing pointer to incomplete type
// RW_queue.c:58: error: dereferencing pointer to incomplete type
// RW_queue.c:62: error: dereferencing pointer to incomplete type
// RW_queue.c:66: error: dereferencing pointer to incomplete type
// RW_queue.c:67: error: dereferencing pointer to incomplete type
// RW_queue.c:72: error: ‘task_struct’ undeclared (first use in this function)
// RW_queue.c:72: error: ‘temp_task’ undeclared (first use in this function)
// RW_queue.c: At top level:
// RW_queue.c:93: warning: data definition has no type or storage class
// RW_queue.c:93: warning: parameter names (without types) in function declaration
// RW_queue.c:95: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘sleeping_task’
// RW_queue.c:97: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘long’
// RW_queue.c:113: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘long’
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment