Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a C macro to sleep? #263

Closed
fluca1978 opened this issue Jun 10, 2022 · 1 comment
Closed

Use a C macro to sleep? #263

fluca1978 opened this issue Jun 10, 2022 · 1 comment

Comments

@fluca1978
Copy link
Collaborator

I think this could improve code readibility: there are several places (not many) where there is a code like fhis:

      else
      { 
      /* Sleep for 1ms */
        struct timespec ts; 
        ts.tv_sec = 0; 
        ts.tv_nsec = 1000000L; 
        nanosleep(&ts, NULL); 

       goto retry; 
      } 

such code performs a sleep and a jump, and is used when asking for a lock acquisition.
Cannot we wrap the code in a macro so that the above becomes:

      else
          SLEEP_AND_GOTO(1000000L,retry)

Could this improve source code readibility and avoid duplication of code?

@jesperpedersen
Copy link
Collaborator

Sure

fluca1978 added a commit to fluca1978/pgagroal that referenced this issue Jun 13, 2022
Introduces two macros:
- `SLEEP` to perform a nanosleep call with the specified amount of
time expressed as nanoseconds;
- `SLEEP_AND_GOTO` does the same of the above but adds also a `goto`
to the specified label.

Example of usage:

      SLEEP_AND_GOTO(1000000L,retry)

is the same as

      {
         /* Sleep for 1ms */
         struct timespec ts;
         ts.tv_sec = 0;
         ts.tv_nsec = 1000000L;
         nanosleep(&ts, NULL);

         goto retry;
     }

Close agroal#263
fluca1978 added a commit to fluca1978/pgagroal that referenced this issue Jun 13, 2022
Introduces two macros:
- `SLEEP` to perform a nanosleep call with the specified amount of
time expressed as nanoseconds;
- `SLEEP_AND_GOTO` does the same of the above but adds also a `goto`
to the specified label.

Example of usage:

      SLEEP_AND_GOTO(1000000L,retry)

is the same as

      {
         /* Sleep for 1ms */
         struct timespec ts;
         ts.tv_sec = 0;
         ts.tv_nsec = 1000000L;
         nanosleep(&ts, NULL);

         goto retry;
     }

Close agroal#263
fluca1978 added a commit to fluca1978/pgagroal that referenced this issue Jun 15, 2022
Introduces two macros:
- `SLEEP` to perform a nanosleep call with the specified amount of
time expressed as nanoseconds;
- `SLEEP_AND_GOTO` does the same of the above but adds also a `goto`
to the specified label.

Example of usage:

      SLEEP_AND_GOTO(1000000L,retry)

is the same as

      {
         /* Sleep for 1ms */
         struct timespec ts;
         ts.tv_sec = 0;
         ts.tv_nsec = 1000000L;
         nanosleep(&ts, NULL);

         goto retry;
     }

Close agroal#263
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants