Include Page |
---|
| fsdoc:css.FilesPOSIX API.cssfsdoc: |
---|
| css.FilesPOSIX API.css |
---|
|
Include Page |
---|
| fsdoc:css.webworks.cssfsdoc: |
---|
| css.webworks.css |
---|
|
Anchor |
---|
| 109162110467211091621 |
---|
| 1046721 |
---|
|
Atomic File Operations Using File Lock
The file module has functions lock files across several operations that are almost exact equivalents to POSIX API functions; the primary difference is the advantage of valuable return error codes to the application. If a file is shared between several tasks in an application, the file lock can be employed to guarantee that a series of file operations are executed atomically. fs_flockfile() (or its non-blocking equivalent fs_ftrylockfile()) acquires the lock for a task (if it does not already own it). Accesses from other tasks will be blocked until fs_funlockfile() is called. Each file actually has a lock count associated with it. This allows nested calls by a task to acquire a file lock; each of those calls must be matched with a call to fs_funlockfile(). HTML Table |
---|
|
Table Row (tr) |
---|
Table Cell (td) |
---|
| void App_Fnct (void) Anchor |
---|
| 109398510480791093985 |
---|
| 1048079 |
---|
| File Module Function { void FSFile_LockGet (FS_FILE *p_file, unsigned char data1[50]; FS_ERR *p_err) unsigned char data2[10]; void FSFile_LockAccept (FS_FILE *p_file, Anchor |
---|
| 1094001 | 1094001 | FS_ERR *p_err); Anchor |
---|
1094005 | 1094005 | void FSFile_LockSet (FS_FILE *p_file, Anchor |
---|
1094009 | 1094009 | FS_ERR *p_err); Table Cell (td) |
---|
| Anchor |
---|
| 1093987 | 1093987 | POSIX API Equivalent
Anchor |
---|
1094038 | 1094038 | void fs_flockfile (FS_FILE *file); Anchor |
---|
1094039 | 1094039 | int fs_ftrylockfile (FS_FILE *file); Anchor |
---|
1094030 | 1094030 | void fs_funlockfile (FS_FILE *file); . . . if (App_FilePtr != (FS_FILE *)0) { fs_flockfile(App_FilePtr); /* Lock file. */ /* See Note #1. */ /* Wr data atomically. */ fs_fwrite(data1, 1, sizeof(data1), App_FilePtr); fs_fwrite(data2, 1, sizeof(data1), App_FilePtr); fs_funlockfile(App_FilePtr); /* Unlock file. */ } . . . }
|
|
...
- Example file lock usage
- fs_flockfile() will block the calling task until the file is available. If the task must write to the file only if no other task is currently accessing it, the non-blocking function fs_funlockfile() can be used.