Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
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

Anchor
109162210467221091622
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.
Anchor
109644410964441046722
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.

Anchor
1046723
1046723
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().

Anchor
1053728
1053728
 

HTML Table
summary
classCode_Listing
Table Row (tr)
FS_ERR *p_err); void FSFile_LockSet (FS_FILE *p_file, FS_ERR *p_err);

POSIX API Equivalent
void fs_flockfile (FS_FILE *file); int fs_ftrylockfile (FS_FILE *file); void fs_funlockfile (FS_FILE *file);
Table Cell (td)
rowspan720

Anchor
1048078
1048078
void App_Fnct (void)

Anchor
109398510480791093985
1048079
File Module Function {

Anchor
109398910480801093989
void FSFile_LockGet (FS_FILE *p_file,
1048080
unsigned char data1[50];

Anchor
109399310480811093993
FS_ERR *p_err)
1048081
unsigned char data2[10];

Anchor
10939971093997
void FSFile_LockAccept (FS_FILE *p_file,
Anchor
10940011094001
Anchor
10940051094005
Anchor
10940091094009
Table Cell (td)
rowspan7

Anchor
10939871093987
Anchor
10940381094038
Anchor
10940391094039
Anchor
10940301094030
1048082
1048082
.

Anchor
1048083
1048083
.

Anchor
1048084
1048084
.

Anchor
1048085
1048085

Anchor
1048086
1048086
if (App_FilePtr != (FS_FILE *)0) {

Anchor
1048087
1048087
fs_flockfile(App_FilePtr); /* Lock file. */

Anchor
1048088
1048088
/* See Note #1. */

Anchor
1048089
1048089
/* Wr data atomically. */

Anchor
1048090
1048090
fs_fwrite(data1, 1, sizeof(data1), App_FilePtr);

Anchor
1051065
1051065
fs_fwrite(data2, 1, sizeof(data1), App_FilePtr);

Anchor
1051066
1051066
fs_funlockfile(App_FilePtr); /* Unlock file. */

Anchor
1051067
1051067
}

Anchor
1048094
1048094
.

Anchor
1048095
1048095
.

Anchor
1048096
1048096
.

Anchor
1048097
1048097
}

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

Table Row (tr)

    1. Anchor

...

    1. 1046724
      1046724
      Example file lock usage
      1. Anchor
        1046745
        1046745
        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.