/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pcilib/locking.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-09-24 02:28:45 UTC
  • mfrom: (305.1.19 views)
  • Revision ID: csa@suren.me-20150924022845-p7hc8lh8v0q48g0r
Finalyze XML support and provide initial support for views (only descriptions so far)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    pcilib_kmem_reuse_state_t reused;
22
22
 
23
23
    assert(PCILIB_LOCK_PAGES * PCILIB_KMEM_PAGE_SIZE >= PCILIB_MAX_LOCKS * PCILIB_LOCK_SIZE);
24
 
 
 
24
        
 
25
        /*protection against multiple creations of kernel space*/
25
26
    err = pcilib_lock_global(ctx);
26
27
    if (err) return err;
27
28
 
 
29
        /* by default, this kernel space is persistent and will be reused, in order to avoid the big initialization times for robust mutexes each time we run pcitool*/
28
30
    ctx->locks.kmem = pcilib_alloc_kernel_memory(ctx, PCILIB_KMEM_TYPE_PAGE, PCILIB_LOCK_PAGES, PCILIB_KMEM_PAGE_SIZE, 0, PCILIB_KMEM_USE(PCILIB_KMEM_USE_LOCKS,0), PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_PERSISTENT);
29
31
    if (!ctx->locks.kmem) {
30
32
        pcilib_unlock_global(ctx);
46
48
        }
47
49
    }
48
50
 
 
51
        /* the lock that has been used for the creation of kernel space is declared unlocked, has we shouldnot use it anymore*/
49
52
    ctx->locks.locking = pcilib_get_lock(ctx, PCILIB_LOCK_FLAG_UNLOCKED, "locking");
50
53
 
51
54
    pcilib_unlock_global(ctx);
59
62
}
60
63
 
61
64
/*
62
 
 * this functions destroy all locks and then free the kernel memory allocated for them
 
65
 * this function free the kernel memory allocated for them and destroys locks by setting memory to 0
63
66
 */
64
67
void pcilib_free_locking(pcilib_t *ctx) {
65
68
    if (ctx->locks.locking)
75
78
int pcilib_lock_global(pcilib_t *ctx) {
76
79
    int err;
77
80
    
78
 
    /* we flock() to make sure to not have two initialization in the same time (possible long time to init) */
 
81
    /* we flock() on the board's device file to make sure to not have two initialization in the same time (possible long time to init) */
79
82
    if ((err = flock(ctx->handle, LOCK_EX))==-1) {
80
83
        pcilib_error("Can't get flock on device file");
81
84
        return PCILIB_ERROR_FAILED;
105
108
    pcilib_lock_t *lock;
106
109
    char buffer[PCILIB_LOCK_SIZE];
107
110
 
108
 
 
 
111
        /* we construct the complete lock_id given the parameters of the function*/
109
112
    va_list pa;
110
113
    va_start(pa, lock_id);
111
114
    ret = vsnprintf(buffer, PCILIB_LOCK_SIZE, lock_id, pa);
115
118
        pcilib_error("Failed to construct the lock id, probably arguments does not match the format string (%s)...", lock_id);
116
119
        return NULL;
117
120
    }
118
 
 
 
121
        
 
122
        
 
123
        /* we iterate through locks to see if there is one already with the same name*/ 
119
124
        // Would be nice to have hash here
120
125
    for (i = 0; i < PCILIB_MAX_LOCKS; i++) {
121
126
        lock = pcilib_get_lock_by_id(ctx, i);
141
146
                }
142
147
            }
143
148
#endif /* ! HAVE_STDATOMIC_H */
 
149
        /* if yes, we increment its ref variable*/
144
150
            pcilib_lock_ref(lock);
145
151
#ifndef HAVE_STDATOMIC_H
146
152
            if ((flags&PCILIB_LOCK_FLAG_UNLOCKED)==0)
192
198
        return NULL;
193
199
    }
194
200
 
 
201
        /* if the lock did not exist before, then we create it*/
195
202
    err = pcilib_init_lock(lock, flags, buffer);
196
203
    
197
204
    if (err) {
229
236
}
230
237
 
231
238
 
232
 
/**
233
 
  * Destroy all existing locks. This is unsafe call as this and other running applications
234
 
  * will still have all initialized lock pointers. It is user responsibility to issue this
235
 
  * command when no other application is running.
236
 
  */
 
239
/*
 
240
 * Destroy all existing locks. This is unsafe call as this and other running applications
 
241
 * will still have all initialized lock pointers. It is user responsibility to issue this
 
242
 * command when no other application is running.
 
243
 */
237
244
int pcilib_destroy_all_locks(pcilib_t *ctx, int force) {
238
245
    int err;
239
246
    pcilib_lock_id_t i;
269
276
        return 0;
270
277
    }
271
278
 
 
279
        /* if we run in non-forced case, then if it may be still processes that can have access to the locks, they are not destroyed*/
272
280
    if (!force) {
273
281
        for (i = 0; i < PCILIB_MAX_LOCKS; i++) {
274
282
            pcilib_lock_t *lock = pcilib_get_lock_by_id(ctx, i);