Enumerations | Functions

/home/sindre/dev_uclinux/suzaku_shared/hwos_sw/libhwos/hmqueue.h File Reference

[Hardware OS Message Queue Interface] Functionality for keeping message queue identifiers. More...

Go to the source code of this file.

Enumerations

enum  hmqueue_mode { HMQ_CREATE, HMQ_CONNECT }
 

Types of modes when creating a message queue.


Functions

struct hmqueuehmqueue_get_prev (struct hmqueue *element)
 Get previous element in list.
struct hmqueuehmqueue_get_next (struct hmqueue *element)
 Get next element in list.
int hmqueue_get_key (struct hmqueue *element)
 Get key of a given message queue.
int hmqueue_get_id (struct hmqueue *element)
 Get ID of message queue.
int hmqueue_size ()
 Get size of elements in message queue list.
int hmqueue_get_id_by_key (int key)
 Get ID of message queue identified by key.
struct hmqueuehmqueue_get (int key)
 Get message queue element identified by key.
struct hmqueuehmqueue_add (int key, enum hmqueue_mode mode)
 Adds a message queue and returns the queue identifier.
int hmqueue_remove (struct hmqueue *element)
 Remove the given list element.
int hmqueue_remove_by_key (int key)
 Remove the given message queue by key.
int hmqueue_remove_all ()
 Remove all message queues in list.
struct hmqueuehmqueue_get_first ()
 Get first element in list.
struct hmqueuehmqueue_get_last ()
 Get last element in list.
int hmqueue_get_daemonkey ()
 Get the key of the message servers message queue.

Detailed Description

[Hardware OS Message Queue Interface] Functionality for keeping message queue identifiers.

This is simply a double linked list of System V message queues. This module should probably be rewritten so it uses hlist internally.

Original author (2010): Vegard Endresen

Modified (2011): Sindre Hansen

Definition in file hmqueue.h.


Function Documentation

struct hmqueue* hmqueue_add ( int  key,
enum hmqueue_mode  mode 
) [read]

Adds a message queue and returns the queue identifier.

Based on "mode" this function either connects to an exisiting message queue or creates an new message queue.

Parameters:
key Unique key for the new queue.
mode Mode for adding a message queue to the list.
Returns:
The message queue struct for the new queue on success. NULL on failure.

Definition at line 184 of file hmqueue.c.

{
        // Remove any existing queues (should be none or just one).
        int nr_removed = 0;
        while (hmqueue_remove_by_key(key) == 0) {
                nr_removed += 1;
                if (nr_removed > 10)
                        return NULL;
        }

        int msgflg = -1;
        if (mode == HMQ_CREATE)
                msgflg = IPC_CREAT | 0666;
        else if (mode == HMQ_CONNECT)
                msgflg = 0666;
        else
                return NULL;

        struct hmqueue* queue = calloc(1, sizeof(*queue));
        int msqid = create_mqueue_(key, msgflg);

        set_key_(queue, key);
        set_id_(queue, msqid);
        set_next_(queue, NULL);
        set_prev_(queue, NULL);

        if(hmqueue_size() == 0) {
                // This is first element.
                firstq = queue;
                lastq  = queue; 
        } else {
                // There is element(s) in list already.
                set_next_(hmqueue_get_last(), queue);
                set_prev_(queue, hmqueue_get_last());
                lastq = queue;
        }

        queue_cnt++;

        return queue;
}

struct hmqueue* hmqueue_get ( int  key  )  [read]

Get message queue element identified by key.

Parameters:
key A key that identifies the message queue.
Returns:
List element on success. NULL on failure.

Definition at line 144 of file hmqueue.c.

{
        if (hmqueue_size() == 0)
                return NULL;

        struct hmqueue* queue;
        queue = hmqueue_get_first();
        if (hmqueue_get_key(queue) == key)
                return queue;

        while (hmqueue_get_next(queue) != NULL) {
                queue = hmqueue_get_next(queue);
                if (hmqueue_get_key(queue) == key)
                        return queue;
        }

        return NULL;
}

int hmqueue_get_daemonkey (  ) 

Get the key of the message servers message queue.

Returns:
The unique key of the message server.

Definition at line 319 of file hmqueue.c.

{
        // Unique key for main message queue.
        return 1234;
}

struct hmqueue* hmqueue_get_first (  )  [read]

Get first element in list.

Returns:
The first list element.

Definition at line 309 of file hmqueue.c.

{
        return firstq; 
}

int hmqueue_get_id ( struct hmqueue element  ) 

Get ID of message queue.

Parameters:
element Pointer to list element that has the message queue.
Returns:
ID of the message queue on success. Negative on failure.

Definition at line 135 of file hmqueue.c.

{
        if (element == NULL)
                return -1;

        return element->id;
}

int hmqueue_get_id_by_key ( int  key  ) 

Get ID of message queue identified by key.

Parameters:
key A key that identifies the message queue.
Returns:
ID of the message queue on success. Negative on failure.

Definition at line 163 of file hmqueue.c.

{
        if(hmqueue_size() == 0)
                return -1;

        struct hmqueue *it;
        it = hmqueue_get_first();       
        if (hmqueue_get_key(it) == key)
                return hmqueue_get_id(it);
        while(1) {
                //last item
                if(hmqueue_get_next(it) == NULL)
                        return -1; 

                it = hmqueue_get_next(it);
                if (hmqueue_get_key(it) == key)
                        return hmqueue_get_id(it);      
        }       
}

int hmqueue_get_key ( struct hmqueue element  ) 

Get key of a given message queue.

Parameters:
element Pointer to list element that has the message queue.
Returns:
The key (positive integer) on success. Negative on failure.

Definition at line 126 of file hmqueue.c.

{
        if (element == NULL)
                return -1;

        return element->key;
}

struct hmqueue* hmqueue_get_last (  )  [read]

Get last element in list.

Returns:
The last list element.

Definition at line 314 of file hmqueue.c.

{
        return lastq; 
}

struct hmqueue* hmqueue_get_next ( struct hmqueue element  )  [read]

Get next element in list.

Parameters:
element Pointer to element that has the next field.
Returns:
Pointer to the next element on success. NULL on failure.

Definition at line 117 of file hmqueue.c.

{
        if (element == NULL)
                return NULL;

        return element->next;
}

struct hmqueue* hmqueue_get_prev ( struct hmqueue element  )  [read]

Get previous element in list.

Parameters:
element Pointer to element that has the previous field.
Returns:
Pointer to the previous element on success. NULL on failure.

Definition at line 108 of file hmqueue.c.

{
        if (element == NULL)
                return NULL;

        return (struct hmqueue*)(element->prev);
}

int hmqueue_remove ( struct hmqueue element  ) 

Remove the given list element.

Parameters:
element Pointer to list element that has the message queue.
Returns:
0 on success. Negative on failure.

List is invalid.

Connect next and previous element together.

Definition at line 227 of file hmqueue.c.

{
        if (element == NULL)
                return -1;

        hlog_write(HLOG_DEBUG, "hmqueue_remove: Removing queue with id=");
        hlog_write_integer(hmqueue_get_id(element));
        hlog_write_text("\n");
        // Remove the actual message queue.
        if (hmqueue_get_id(element) > 0)
                msgctl(hmqueue_get_id(element), IPC_RMID, NULL);

        if (hmqueue_size() < 1) {
                hlog_write(HLOG_ERROR, "hmqueue_remove: Invalid list structure (-2).\n");
                return -2;
        } else if (hmqueue_size() == 1) {
                // Only one element.
                free(element);
                firstq = NULL;
                lastq = NULL;
                queue_cnt = 0;
        } else {
                // More than one element.
                struct hmqueue* element_prev = hmqueue_get_prev(element);
                struct hmqueue* element_next = hmqueue_get_next(element);
                if (element_prev == NULL && element_next != NULL) {
                        // This is first element.
                        firstq = element_next;
                        set_prev_( element_next, NULL );
                } else if (element_next == NULL && element_prev != NULL) {
                        // This is last element.
                        lastq = element_prev;
                        set_next_( element_prev, NULL);
                } else if (element_next != NULL && element_prev != NULL) {
                        // Element is in-between.

                        set_prev_( element_next, element_prev );
                        set_next_( element_prev, element_next );

                } else {
                        // List is invalid.
                        hlog_write(HLOG_ERROR, "hmqueue_remove: Invalid list structure (-3).\n");
                        return -3;
                }

                free(element);
                queue_cnt--;
        }

        return 0;
}

int hmqueue_remove_all (  ) 

Remove all message queues in list.

Returns:
0 on success. Negative on failure.

Definition at line 289 of file hmqueue.c.

{
        struct hmqueue* it = hmqueue_get_first();
        struct hmqueue* it_prev = NULL;

        int ret = 0;
        while (hmqueue_get_next(it) != NULL) {
                it = hmqueue_get_next(it);
                it_prev = hmqueue_get_prev(it);
                if (hmqueue_remove(it_prev) < 0)
                        ret = -1;
        }

        if (hmqueue_remove (it) < 0)
                ret = -1;

        return ret;
}

int hmqueue_remove_by_key ( int  key  ) 

Remove the given message queue by key.

Parameters:
key Key that identifies the message queue.
Returns:
0 on success. Negative on failure.

Definition at line 282 of file hmqueue.c.

{
        struct hmqueue* element = hmqueue_get(key);
        return hmqueue_remove(element); 
}

int hmqueue_size (  ) 

Get size of elements in message queue list.

Returns:
Number of elements in list.

Definition at line 102 of file hmqueue.c.

{
        return queue_cnt;
}

 All Data Structures Files Functions Variables Enumerations Enumerator Defines