Changeset c1c0184 in mainline for uspace/lib/c/generic/async_sess.c


Ignore:
Timestamp:
2010-12-26T17:18:36Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade
Children:
d3cce52
Parents:
7907cf9
Message:

Make session management explicit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/async_sess.c

    r7907cf9 rc1c0184  
    107107#include <assert.h>
    108108
    109 #define SESSION_HASH_BUCKETS    16
    110 
    111 typedef struct {
    112         link_t link;            /**< Session hash table link. */
    113         int sess_phone;         /**< The phone serving as session identifier. */
    114         link_t conn_head;       /**< List of open connections. */
    115 } sess_node_t;
    116 
    117109typedef struct {
    118110        link_t conn_link;       /**< Link for the list of connections. */
     
    132124
    133125/**
    134  * Hash table mapping session phone IDs to session records.
    135  */
    136 static hash_table_t session_ht;
    137 
    138 static hash_index_t sess_ht_hash(unsigned long *key)
    139 {
    140         return *key % SESSION_HASH_BUCKETS;
    141 }
    142 
    143 static int sess_ht_compare(unsigned long *key, hash_count_t keys, link_t *item)
    144 {
    145         sess_node_t *sess = hash_table_get_instance(item, sess_node_t, link);
    146 
    147         return *key == (unsigned long) sess->sess_phone;
    148 }
    149 
    150 static void sess_ht_remove(link_t *item)
    151 {
    152 }
    153 
    154 static hash_table_operations_t session_hash_ops = {
    155         .hash = sess_ht_hash,
    156         .compare = sess_ht_compare,
    157         .remove_callback = sess_ht_remove
    158 };
     126 * List of all existing sessions.
     127 */
     128//static LIST_INITIALIZE(session_list);
    159129
    160130/** Initialize the async_sess subsystem.
     
    162132 * Needs to be called prior to any other interface in this file.
    163133 */
    164 int async_sess_init(void)
     134void _async_sess_init(void)
    165135{
    166136        fibril_mutex_initialize(&async_sess_mutex);
    167137        list_initialize(&inactive_conn_head);
    168         return hash_table_create(&session_ht, SESSION_HASH_BUCKETS, 1,
    169             &session_hash_ops);
    170 }
    171 
    172 static void sess_node_initialize(sess_node_t *sess)
    173 {
    174         link_initialize(&sess->link);
     138}
     139
     140void async_session_create(async_sess_t *sess, int phone)
     141{
     142        sess->sess_phone = phone;
     143        list_initialize(&sess->conn_head);
     144}
     145
     146void async_session_destroy(async_sess_t *sess)
     147{
    175148        sess->sess_phone = -1;
    176         list_initialize(&sess->conn_head);
     149        /* todo */
    177150}
    178151
     
    186159/** Start new transaction in a session.
    187160 *
    188  * @param sess_phone    Phone representing the session.
     161 * @param sess_phone    Session.
    189162 * @return              Phone representing the new transaction or a negative error
    190163 *                      code.
    191164 */
    192 int async_transaction_begin(int sess_phone)
    193 {
    194         unsigned long key = (unsigned long) sess_phone;
    195         link_t *lnk;
    196         sess_node_t *sess;
     165int async_transaction_begin(async_sess_t *sess)
     166{
    197167        conn_node_t *conn;
    198168        int data_phone;
    199169
    200170        fibril_mutex_lock(&async_sess_mutex);
    201         lnk = hash_table_find(&session_ht, &key);
    202         if (!lnk) {
    203                 /*
    204                  * The session node was not found in the hash table. Try to allocate
    205                  * and hash in a new one.
    206                  */
    207                 sess = (sess_node_t *) malloc(sizeof(sess_node_t));
    208                 if (!sess) {
    209                         /*
    210                          * As a possible improvement, we could make a one-time
    211                          * attempt to create a phone without trying to add the
    212                          * key node into the hash.
    213                          */
    214                         fibril_mutex_unlock(&async_sess_mutex);
    215                         return ENOMEM;
    216                 }
    217                 sess_node_initialize(sess);
    218                 sess->sess_phone = sess_phone;
    219                 hash_table_insert(&session_ht, &key, &sess->link);
    220         } else {
    221                 /*
    222                  * Found the session node.
    223                  */
    224                 sess = hash_table_get_instance(lnk, sess_node_t, link);
    225         }
    226171
    227172        if (!list_empty(&sess->conn_head)) {
     
    242187                 */
    243188retry:
    244                 data_phone = async_connect_me_to(sess_phone, 0, 0, 0);
     189                data_phone = async_connect_me_to(sess->sess_phone, 0, 0, 0);
    245190                if (data_phone >= 0) {
    246191                        /* success, do nothing */
     
    276221/** Finish a transaction.
    277222 *
    278  * @param sess_phone    Phone representing the session.
     223 * @param sess          Session.
    279224 * @param data_phone    Phone representing the transaction within the session.
    280225 */
    281 void async_transaction_end(int sess_phone, int data_phone)
    282 {
    283         unsigned long key = (unsigned long) sess_phone;
    284         sess_node_t *sess;
     226void async_transaction_end(async_sess_t *sess, int data_phone)
     227{
    285228        conn_node_t *conn;
    286         link_t *lnk;
    287229
    288230        fibril_mutex_lock(&async_sess_mutex);
    289         lnk = hash_table_find(&session_ht, &key);
    290         assert(lnk);
    291         sess = hash_table_get_instance(lnk, sess_node_t, link);
    292231        conn = (conn_node_t *) malloc(sizeof(conn_node_t));
    293232        if (!conn) {
Note: See TracChangeset for help on using the changeset viewer.