forward-jt
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 2021q1 Homework5 (quiz5) contributed by < `RZHuangjeff` > ###### tags: `linux2021` > [Problem set](https://hackmd.io/@sysprog/linux2021-quiz5) ## Problem 2 ### Description This is a program that helps to handle coroutines of a process. With coroutine, a program can switch to other routine while the current executing routine is waiting for certain conditions happend, rather than blocks entire program. To be able to resume the execution point of former routine, a GCC extension called [Labels as Values](https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html) performs the key role, since it allows the address of a label being saved and jump (`goto`) to it later, via saved address. The typical use case of coroutine is to maintain I/O operations between different input sources and ouput destinations. Just like the example shown in the quiz, which handles I/O operations between standard I/O and a socket, those operations are executed simultaneously, which means that block waiting on standard input will not cause reading from socket being blocked. For the example shown in quiz, a wait on a condition is performed by macro `cr_wait`, once the condition that it waits for has not happened yet, this macro will return from the current function after address of the checkpoint, a unique label generated automaticaly, is saved. While that function is invoked later, macro `cr_begin` will realize that a checkpoint was saved and will jump to that checkpoint to resume execution. By wrapping several routines that should be executed simultaneously in a loop, next routine will be invoked while previous one is waiting. ### Improvements > commit [c8d6f79](https://github.com/RZHuangJeff/quiz5/commit/c8d6f790697aa88f4781748cb3240e78d37723b0) The original version of coroutine implemented in quiz 5 has basic functions that performs coroutine, but there are several cons I discovered: * **misuse macros** With the origin definition of macros, one may writes program as following: ```cpp= void main_loop() { struct cr routine1 = cr_init(); struct cr routine2 = cr_init(); while (cr_state(&routine1) != CR_FINISHED && cr_state(&routine2) != CR_FINISHED) { cr_begin(&routine1); ... cr_wait(&routine1, /* cond to wait */); ... cr_end(&routine1); cr_begin(&routine2); /*body of routine2 */ cr_end(&routine2); } } ``` which may lead to unexpected result since once the condition being waited in line 10 has not happened yet, the program will return from `main_loop`, rather than execute next routine, although such program makes sence. To avoid such misusing, using of such macros should be limited in particular functions. To fulfill that, the argument `o` of all macros that should be limited are removed, instead, the context of coroutine mentioned in those macros is hardcoded as `__ct`. A macro `cr_func_def` is introduced that is used when someone wants to define a coroutine function, those functions contain a single argument named `__ct` that represents context corresponding to that routine. Following is definition of `cr_func_def` macro: ```cpp #define cr_func_name(name) cr_func_##name #define cr_func_def(name) \ static void cr_func_name(name)(struct cr *__ct) ``` And one may define a routine like: ```cpp= cr_func_def(sample_routine) { /* initializations */ cr_begin(); /* body of routine */ cr_end(); /* finalizations */ } ``` With these changes, compiler will report errors for the program shown above. * **`cr_context` and `cr_run`** The macro `cr_context` helps to declare a `struct cr` variable, which acts as context of specific coroutine. The macro `cr_run` is a wrapper that wraps invocation of coroutine function with its context. If there is no declaration of such coroutine or context for such coroutine, a compilation error will be reported, this helps one to make sure that all required job was done. Following are definitions of macros mentioned above: ```cpp #define cr_context_name(name) cr_context_##name #define cr_context(name) struct cr cr_context_name(name) #define cr_run(name) cr_func_name(name)(&cr_context_name(name)) ``` * **field local** In the definition of `struct cr`, we can find that there is a pointer named `local`, which is declared but not used. Since we have introduced a series of macro that help defining routine functions, with this definition of routine function, there is no way to pass arguments to these functions. To deal with such problem, the macro `cr_init` is modified and renamed as `cr_context_init` which accepts a pointer that will be assigned to `local`. To reference variable that is pointed by `local`, one may use one of following two macros: `cr_priv_var` or `cr_grab_priv_var`, the first macro will cast `__ct->local` to a pointer of given type, while second one will return a pointer that points to the given field with in the structure that `__cr->local` points to. Following are definitions of those macros: ```cpp #define cr_grab_priv_var(type, memb) &((type*) (__ct->local))->memb #define cr_priv_var(type) ((type *) (__ct->local)) ``` * **`cr_local` macro** A macro `cr_local` is introduced to wrap declaration of static local variables, which aims to highlight that such variable is a part of coroutine. Following is definition of `cr_local`: ```cpp #define cr_local static ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully